I Use this code to upload an image to server and storing in database :
thought the database column is binary(max) , when i upload a filed and try to read it from the database only a partial amount of the total byte array (file stream) is saved into the database.
public virtual ActionResult CreateService(Service service, HttpPostedFileBase serviceImage )
{
if (ModelState.IsValid)
{
if (serviceImage != null)
{
var target = new MemoryStream();
serviceImage.InputStream.CopyTo(target);
service.Image = target.ToArray();
}
_serviceTask.AddNewItem(service); // Inserting new entity using NHibernate
}
return RedirectToAction("Index");
}
Everything works fine but when I try to retrive it from database and displaying it to user I get incomplete image. for example user uploads this image :
But when I retrive it I get
:
I've set database image column to varbinary(max) I also tried image datatype . and here is my form code :
@using (Html.BeginForm("CreateService", "Services", FormMethod.Post, new { @id = "dialogForm", @class = "mws-form", @enctype = "multipart/form-data" }))
{
<input type="file" name="serviceImage" />
}
How can I fix it ?