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.

10
  • What browser are you using? is that an intranet application? are you running it witn IIS? Commented Jun 25, 2012 at 18:39
  • did you try to upload a small file (example 10 kb)? Commented Jun 25, 2012 at 21:14
  • Can you see how does the database stored large files? Were not like 0x0000000, weren't they? Commented Jun 25, 2012 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. Commented Jun 26, 2012 at 14:17
  • Ok. 0x0000 is null. Maybe this will help: stackoverflow.com/questions/10955051/… Commented Jun 26, 2012 at 14:36

3 Answers 3

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.
Sign up to request clarification or add additional context in comments.

1 Comment

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();
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.

2 Comments

Didn't fix the issue. Also, do both of those values represent 50MB? Could you explain their units?
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
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);
      }
 }

1 Comment

I am not only trying to save the image but specifically trying to store and retrieve from DB using NHibernate model binding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.