61

I have a problem when I upload a file in ASP.NET MVC. My code is below:

View:

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index2</h2>
@using (Html.BeginForm("FileUpload", "Board", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
  <input type="file" />
  <input type="submit" />
}

Controller:

[HttpPost]
public ActionResult FileUpload(HttpPostedFileBase uploadFile)
{
    if (uploadFile != null && uploadFile.ContentLength > 0)
    {
        string filePath = Path.Combine(Server.MapPath("/Temp"), Path.GetFileName(uploadFile.FileName));
        uploadFile.SaveAs(filePath);
    }
    return View();
}

But uploadFile always returns null. Can anyone figure out why??

tereško
  • 56,151
  • 24
  • 92
  • 147
Joshua Son
  • 1,721
  • 6
  • 27
  • 46

6 Answers6

129
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index2</h2>
@using (Html.BeginForm("FileUpload", "Board", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
  <input type="file" name="uploadFile"/>
  <input type="submit" />
}

you have to provide name to input type file to uploadFile in order to model binding work in ASP.net mvc and
also make sure that name of your input type file and argument name of HttpPostedFileBase is identical.

Frank Myat Thu
  • 4,172
  • 9
  • 61
  • 112
dotnetstep
  • 14,826
  • 4
  • 49
  • 61
10

I had tried most of the solutions posted online for this topic, but found it better to use a workaround instead..

It really didn't matter what I did the HttpPostedFileBase and/or HttpPostedFile were always null. Using the HttpContext.Request.Files collection seemed to work with no hassles at all.

e.g.

 if (HttpContext.Request.Files.AllKeys.Any())
        {
            // Get the uploaded image from the Files collection
            var httpPostedFile = HttpContext.Request.Files[0];

            if (httpPostedFile != null)
            {
                // Validate the uploaded image(optional)

                // Get the complete file path
                var fileSavePath =(HttpContext.Server.MapPath("~/UploadedFiles") + httpPostedFile.FileName.Substring(httpPostedFile.FileName.LastIndexOf(@"\")));

                // Save the uploaded file to "UploadedFiles" folder
                httpPostedFile.SaveAs(fileSavePath);
            }
        }

In the above example I only grab the first file, but it is just a matter of looping though the collection to save all files.

HTH

Rob

roblem
  • 395
  • 3
  • 7
5

In my scenario the problem was with id attribute, I had this:

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

The soultion was to remove id:

<input type="file" name="file1"  />
pawel
  • 51
  • 1
  • 2
  • 3
    Damm, in my case, I was using HttpPostedFile instead of HttpPostedFileBase.. programmer error :/ – Samuel Diogo Oct 21 '15 at 16:13
  • Samuel you saved my day! Although it is explained at http://stackoverflow.com/a/24911221/2346618 I think it is silly to use a base class HttpPostedFileBase in signature. In object oriented programming, base classes are used to help construction of concrete classes and interfaces are used for protocols. Using an interface like IHttpPostedFile will be less error-prone IMO. – Gökçer Gökdal Jan 31 '16 at 17:36
3

While not the answer to this specific user, I would like to point out that HTML requires that the form tag has an enctype attribute with the value multipart/form-data. And of course both the attribute and it's value must be correct.

For mvc, this means that when using beginform, you should use the version with the htmlAttributes parameter

jmoreno
  • 12,480
  • 2
  • 50
  • 83
  • I found this to be really helpful, and loaded with information on enctype [https://stackoverflow.com/questions/4526273/what-does-enctype-multipart-form-data-mean](https://stackoverflow.com/questions/4526273/what-does-enctype-multipart-form-data-mean). It's a great explanation. – Greg Barth Jun 20 '18 at 21:32
0

There can be another scenario also. In my case, I was getting this issue because I was directly rendering script tag in my MVC view and IE is giving issue there.

Correct code in view should be as below:

@section scripts
{
    <script>
        $(document).ready(function () {
            $('.fileinput').fileinput();
...
}
sandeep talabathula
  • 3,008
  • 3
  • 26
  • 37
0

You need to use Razor code to set Name of the input file.

<input type="file" name="@Html.Namefor(m => m.propertyName)">