1

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 : Uploaded image But when I retrive it I get this image :
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 ?

Shahin
  • 12,543
  • 39
  • 127
  • 205
  • Instead of *binay(max)* can u change the column as type *image*? – VJAI Jul 04 '12 at 03:34
  • 2
    I suspect that the problem is in your DAL layer. What happens if instead of saving the image to the database, for testing purposes, you save it to the file system: `File.WriteAllBytes(@"c:\test.jpg", target.ToArray());`. – Darin Dimitrov Jul 04 '12 at 06:39

2 Answers2

1

As Darin Dimitrov stated the problem is in DAL. Assuming you are using NHibernate 3.2 and SQL 2008 you must check database field type and NHibernate mapping. Database field type must be set to Varbinary(MAX) and the mapping must explicitly declare column length.

Just like following (mapping by code):

    internal class ServiceOverride : IOverride
{
    public void Override(ModelMapper mapper)
    {
        mapper.Class<Service>(map =>
                                  {
                                      map.Property(x => x.Image,
                                                   status => status.Column(c =>
                                                   {
                                                       c.SqlType("VARBINARY(MAX)");
                                                       c.Length(int.MaxValue);
                                                   }));
                                  });
    }
}

For more information visit Binary Blob truncated to 8000 bytes - SQL Server 2008 / varbinary(max) and Storing images in database results in partial image

Community
  • 1
  • 1
Afshar Mohebi
  • 10,479
  • 17
  • 82
  • 126
0

Maybe you should you GetBuffer(), which returns a byte[]

http://msdn.microsoft.com/en-us/library/system.io.memorystream.getbuffer.aspx

In the remarks: Note that the buffer contains allocated bytes which might be unused.

The ToArray omits those bytes.

sksallaj
  • 3,872
  • 3
  • 37
  • 58