我想将使用Imageresizer插件调整大小的图像保存到数据库。 这是我的代码。
<HttpPost()>
<ValidateAntiForgeryToken()>
Async Function Edit(ByVal employeeMaster As EmployeeMaster, ByVal upload As HttpPostedFileBase) As Task(Of ActionResult)
Try
If ModelState.IsValid Then
If upload IsNot Nothing Then
'Dim imageData As Byte()= New [Byte](upload.InputStream.Length - 1) {} // this section saving to database which is original file ----- start
'upload.InputStream.Read(imageData, 0, CInt(upload.InputStream.Length))
'employeeMaster.Photo = imageData // --- end
Dim guid1 As String = Guid.NewGuid().ToString()
Dim filename = guid1
Dim versions = New Dictionary(Of String, String)()
Dim path1 = Server.MapPath("~/Content/")
versions.Add("", "maxwidth=600&maxheight=600&format=jpg")
For Each suffix As String In versions.Keys
upload.InputStream.Seek(0, SeekOrigin.Begin)
ImageBuilder.Current.Build(New ImageJob(upload.InputStream, path1 + filename, New Instructions(versions(suffix)), False, True))
Next
Dim savedFileName = Path.Combine(path1, Path.GetFileName(filename))
Return File(savedFileName, "application/octet-stream", filename)
upload.SaveAs(savedFileName)
Dim imageData As Byte()= New [Byte](upload.InputStream.Length - 1) {} // trying to save new image to database ----- start
upload.InputStream.Read(imageData, 0, CInt(upload.InputStream.Length))
employeeMaster.Photo = imageData // -----end
Dim size1 = upload.ContentLength
Dim fullPath As String = Request.MapPath("~/Content/" + filename)
If System.IO.File.Exists(fullPath) Then
Dim ex = "ok"
System.IO.File.Delete(fullPath)
End If
End If
db.Entry(employeeMaster).State = EntityState.Modified
Await db.SaveChangesAsync()
Dim json1 = New With {Key .message = "Updated Successfully", Key .error = "False"}
Return Json(json1, JsonRequestBehavior.AllowGet)
Else
'logic for when that description exists
'for example, to add to ModelState
ModelState.AddModelError("EmployeeCode", "EmployeeCode Exists")
End If
Catch ex As DbEntityValidationException
End Try
End Function
上传的文件可以直接保存到数据库。但我正在使用Imageresizer插件压缩该图像。然后它保存到一个文件夹。我正在尝试读取文件夹中保存的图像并将新形成的图像保存到数据库。怎么可以呢?
答案 0 :(得分:0)
我已经解决了这个问题。 这是代码
Dim _fileInfo As New System.IO.FileInfo(savedFileName)
Dim _NumBytes As Long = _fileInfo.Length
Dim _FStream As New System.IO.FileStream(savedFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read)
'upload.SaveAs(savedFileName)
imageData = New [Byte](_FStream.Length - 1) {}
_FStream.Read(imageData, 0, CInt(_FStream.Length))
employeeMaster.Photo = imageData