2

EDIT:

Here's a create script for the table minus the constraints on the keys for brevity.

CREATE TABLE [dbo].[ProfileFile](
[ID] [int] IDENTITY(1,1) NOT NULL,
[ProfileID] [int] NOT NULL,
[FileTypeId] [int] NOT NULL,
[Data] [image] NOT NULL

EDIT:

Attempt to bypass NHibernate to access data in service layer

byte[] temp = (byte[])this._baseModelRepository.CurrentSession.CreateSQLQuery("SELECT Data FROM ProfileFile WHERE ProfileID = :ID").SetParameter("ID", id).UniqueResult();

EDIT: After checking binary data saved in db, it seems that the entire file is uploaded and that my issue is actually on the side of displaying the image.

The image is not rendered but I can follow the action link to download the file but it is cut off at 8KB.

ProfileFile Model class Data field as generated by NHibernate

public virtual System.Byte[] Data { get; set; }

I hard coded the MIME type and file name for now while getting this set up.

public ActionResult GetProfilePhotoByID(int profileID)
    {
        var profileFile= //Load file object from DB by profileID
        byte[] byteArray = profileFile.Data;
        string mimeType = "image/jpg";
        string fileName = "ProfilePhoto.JPG";

        return File(byteArray, mimeType, fileName);
    }

Using debugger I found that the size of profileFile.Data is 8000. Maybe this is a NHibernate restriction?

Also, this is running with IIS and I am getting the same effect in Chrome, Firefox, and IE but am mainly testing in Chrome.

Original question: I am trying to allow the user to upload a profile photo which I will then later serve up on their profile page. Unfortunately, the image file is not completely uploaded and is cut off at 8 KB. Below are simplified versions of some of the more important code segments.

Index View snippets

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "profileform", enctype = "multipart/form-data" }))

<input type="file" id="photo" name="photo" />

Controller post action has HttpPostedFileBase photo parameter

byte[] input;
            if (photo != null && photo.ContentLength > 0)
            {
                input = new byte[photo.ContentLength];
                photo.InputStream.Read(input, 0, photo.ContentLength);


                if (model.ProfileFiles == null)
                {
                    model.ProfileFiles = new List<Core.Model.ProfileFiles>();
                }

                model.ProfileFiles.Add(new Core.Model.ProfileFiles()
                {
                    ProfileID = model.ID,
                    Profile = model,
                    Data = input
                });
            }

Model class is generated with NHibernate. The relevant field is

// One To Many:
public virtual IList<ProfileFile> ProfileFiles { get; set; }

Just comment if more information is needed.

d456
  • 1,147
  • 4
  • 13
  • 25
  • What browser are you using? is that an intranet application? are you running it witn IIS? – Pablo Claus Jun 25 '12 at 18:39
  • did you try to upload a small file (example 10 kb)? – Pablo Claus Jun 25 '12 at 21:14
  • Can you see how does the database stored large files? Were not like 0x0000000, weren't they? – Pablo Claus Jun 25 '12 at 21:24
  • I tried a 6KB file and the entire thing got through. I think the limit is instated when reading the model property. I'm using an image column in SQL Server and it looks like the format 0x0000000. – d456 Jun 26 '12 at 14:17
  • Ok. 0x0000 is null. Maybe this will help: http://stackoverflow.com/questions/10955051/ii6-file-upload-in-mvc-storing-as-0x0000-in-sql-database – Pablo Claus Jun 26 '12 at 14:36
  • It is in that format but does contain data. 0xFFD8FFE000104A46494 is the first part. The db is receiving and storing the entire file (unlike what I first thought) but it is chopping it off at 8KB when I access it via the NHibernate model bound property in the controller action `GetProfilePhotoByID`. If the file is under 8KB, it gets the whole file without issue. – d456 Jun 26 '12 at 16:20
  • I recommend you this: 1) Test it without using IIS 2)Test it without Hibernate. For discarding propose... – Pablo Claus Jun 26 '12 at 17:52
  • Sorry, I was mistaken. I am using the Visual Studio Development Server, not IIS. I updated question with service layer call to access DB more directly than using NHibernate. Still cut off at 8KB (byte[] limited to 8000). – d456 Jun 27 '12 at 15:38
  • If you are using SQL Server 2005/2008, try changing Image type to varbinary(max) and regenerate ProfileFile Model. – Pablo Claus Jun 27 '12 at 21:17
  • Solved the problem using http://stackoverflow.com/a/11005421/1454576 – d456 Jun 28 '12 at 14:50

3 Answers3

1

Solved the issue, and with using NHibernate still too, using Peter Gluck's answer to a related question.

var persistenceModel = AutoMap.AssemblyOf<Model.BaseModel>()
    .Override<Model.ProfileFile>(map =>
    {
        // Sets max length of byte[] retrieved from image column of DB
        map.Map(x => x.Data).Length(2147483647);
    })
    ... etc.
Community
  • 1
  • 1
d456
  • 1,147
  • 4
  • 13
  • 25
  • FYI, this has been working in IIS in production. Also can tell it that it is nullable. `map.Map(x => x.Data).CustomType("BinaryBlob").Length(1048576).Nullable();` – d456 Nov 06 '12 at 22:37
0

Try setting the following in your Web.config

<system.web>
    <!--50MB-->
    <httpRuntime maxRequestLength="51200"/>
</system.web>

and

<system.webServer>
    <security>
        <requestFiltering>
            <!--50MB-->
            <requestLimits maxAllowedContentLength="52428795" />
        </requestFiltering>
    </security>
</system.webServer>

For an explanation of maxRequestLength and maxAllowedContentLength see this question.

Community
  • 1
  • 1
Kevin Aenmey
  • 13,259
  • 5
  • 46
  • 45
  • Didn't fix the issue. Also, do both of those values represent 50MB? Could you explain their units? – d456 Jun 25 '12 at 18:38
  • Yes, they do both represent 50MB. Honestly, I don't know why they chose different units. Maybe someone else can fill us in on why. One setting is an ASP.NET setting, the other is an IIS setting. maxRequestLength is in KB whereas maxAllowedContentLength is in bytes – Kevin Aenmey Jun 25 '12 at 18:40
0
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
  <input id="file" type="file" name="file" />
  <input type="submit" value="Upload" class="btn" />
}

 public ActionResult ChangePhoto(HttpPostedFileBase file)
{
    if (file != null && file.ContentLength > 0)
    {

      using (System.Drawing.Image Img = System.Drawing.Image.FromStream(file.InputStream))
      {
         Img.Save(Server.MapPath("~") +"/myimage.png", Img.RawFormat);
      }
 }
Ali Adravi
  • 21,707
  • 9
  • 87
  • 85
  • I am not only trying to save the image but specifically trying to store and retrieve from DB using NHibernate model binding. – d456 Jun 25 '12 at 19:03