1

I'm using the HTML5 File API features to create beatiful file uploader for my website.

But I've got some problems with the server side (where I use ASP.NET MVC 4).

I couldn't see any file data at the breakpoint in VS, it's just always equaled to the null value. Breakpoint is firing (so by this logic, there is a request), but there isn't any data.

I've read that for the argument it the POST method, which does accept a file must be the idential ID value, which was set in HTML markup of your uploader page.

Let's look at my code:

Server-side:

[HttpPost]
public ActionResult Upload(HttpPostedFileBase fileData)
{
    if (fileData != null && fileData.ContentLength > 0)
    {
        var fileName = System.IO.Path.GetFileName(fileData.FileName);
        var path = System.IO.Path.Combine(@"C:\tmp", fileName);
        fileData.SaveAs(path);
    }

    return RedirectToAction("../NewContract/Strict");
}

Client side (mark-up):

http://ideone.com/o2msT1

Client side (JS):

http://ideone.com/xAJy3i

How can I fix my issue?

  • Check this out .. this might help http://stackoverflow.com/questions/10856240/asp-mvc-file-upload-httppostedfilebase-is-null – Guanxi Jul 15 '13 at 16:05

2 Answers2

1

Change:

Content-Disposition: form-data; name='myFile'; filename='"

To:

Content-Disposition: form-data; name='fileData'; filename='"

And will work with Model binding.

JayPrime2012
  • 2,672
  • 4
  • 28
  • 44
-2

Don't rely on Model binding:

var length = Request.ContentLength;
var bytes = new byte[length];
Request.InputStream.Read(bytes, 0, length);
BarrySlisk
  • 28
  • 2
  • Or you could make use of Model binding since it is obviously better and just change the code you have here: Content-Disposition: form-data; name='myFile'; filename='" and instead of 'myFile' you'll want to call it fileData as per your api parameter name. – JayPrime2012 Sep 21 '13 at 01:28