htmlasp.net-mvc-4asp.net-mvc-file-upload

Uploading Multiple Files - MVC..is there a total file size limit?


I have a form from which users can add a property listing. Apart from the data that the user has to enter, the user can also upload a number of images. I'm using the HTML5 multiple attribute to allow users to upload more than one file at once.

For some reason the HttpPost Add method I have always fires when I upload one image. However, when I try to upload multiple images the method does not fire. Up till now I'm not getting any errors so I'm not sure what is wrong with my implementation.

HTML

@using (Html.BeginForm("AddProperty", "User", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <fieldset>
        <legend>Property</legend>

        <div class="editor-label">
            Title
        </div>
        <div class="editor-field">
            <input type="text" name="title" />
        </div>

        <div class="editor-label">
            Description
        </div>
        <div class="editor-field">
            <input type="text" name="desc" />
        </div>

        @* ... more input fields... *@

        <div class="editor-label">
            Images
        </div>
        <div class="editor-field">
            <input type="file" multiple="multiple" name="file" />
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

Controller

[HttpPost]
public ActionResult AddProperty(FormCollection form, IEnumerable<HttpPostedFileBase> file)
{
    Property p = new Property();
    p.Title = form["title"];
    p.Description = form["desc"];

    // ... setting Property fields

    new PropertiesBL().AddProperty(p);

    for (int i = 0; i < file.Count(); i++)
    {
        // ... loop and save uploaded file(s)
    }
}

Any help would be appreciated.

EDIT - Further testing

To test the file upload I've created a new view with just a form containing a file uploader. I've tried uploading different file types. What I've noted is that when uploading for example 10 documents the Upload method fired. But when trying to upload 3 (large) images it didn't fire. I'm thinking that there's some kind of limit with regards to the total number of bytes being uploaded...I'm not sure if this makes sense though.

Simple Test

<form method="post" action="/Test/Upload" enctype="multipart/form-data">
    <input type="file" multiple="multiple" name="images" />
    <input type="submit" value="Upload" />
</form>

[HttpPost]
public ActionResult Upload(IEnumerable<HttpPostedFileBase> images)
{
    return RedirectToAction("Index");
}

I've also tried uploading multiple files separately. When I try to upload 3 large images (each image about 2 MB) the method did not fire. I'm almost certain that this is an issue related to some kind of total maximum file size.

<form method="post" action="/Test/Upload" enctype="multipart/form-data">
    <input type="file" name="image_1" />
    <input type="file" name="image_2" />
    <input type="file" name="image_3" />
    <input type="submit" value="Upload" />
</form>

Solution

  • Problem was with the maximum number of bytes that one can upload. This can be changed by setting the maxRequestLength in the web.config file.

    Reference

    How to upload large files using MVC 4?

    Code:

    <system.web>
    
        <httpRuntime targetFramework="4.5" 
            maxRequestLength="2147483647"
            executionTimeout="1600" 
            requestLengthDiskThreshold="2147483647" />
       ....
    
    </system.web>
    

    The answer for the linked question, lists the following under the <system.web> tag however I could only add it in the <system.webServer> tag:

    <system.webServer>
    
        <security>
            <requestFiltering>
                <requestLimits maxAllowedContentLength="2147483647" />
            </requestFiltering>
        </security>
        ....
    
    </system.webServer>