2

I'm using mvc 4 and trying to submit multiple files as part of my model.

My form looks like this

 <% using (Html.BeginForm("Edit", "Issue",FormMethod.Post, new {id = "issueForm", encType="multipart/form-data"})) %>

which renders

<form id="issueForm" method="post" enctype="multipart/form-data" action="/Issue/Edit">

My action looks as following

[Authorize]
[HttpPost]
public ActionResult Edit(IssueViewModel viewmodel)

and the view model has a property on it

public IEnumerable<HttpPostedFileBase> Files { get; set; }

the form contains an input for files like this:

<input id="filesX" type="file" name="Files">

where X is an incrementing number of how many file boxes have been added via javascript.

I've used fiddler and firebug and I can definitly see the files are being sent in the request and this is confirmed also by looking at Request.Files[0] (when sending 1 single file) on the server as it shows the content and the file name.

When I look at the Files property it looks like the following: enter image description here

So it seems like the file collection is being populated with null objects for some reason instead of the actual file data, which is weird. I would think that if the model binding didnt work the entire Files collection would be null rather than the first item in it.

Any ideas as to whats going on here?

EDIT: I've also tried having the files as a parameter in the action method (it started off this way actually), tried changing the case of the files input name both of which had no effect.

EDIT 2: It seems to submit fine in IE but not in firefox or chrome

EDIT 3: Seems that in FireFox/Chome the X-Requested-With header is sent with "XMLHttpRequest". I suspect something funky is going on with the ajaxform plugin we are using and its not creating its iframe to do the submissions properly

EDIT 4: I've reproduced most of the code in a separate project and it all seems to work, no idea why it wont in this scenario must be something weird going on. Only thing I've been able to track down is that any browser that doesnt support xmlhttprequest level 2 seems to cause the file to get bound correctly.

7
  • 1
    please add your entire View to your question Feb 5, 2013 at 3:16
  • Please check this blog post from Phil haack. It may help Feb 5, 2013 at 4:44
  • yeah I have followed all of those suggestions Feb 5, 2013 at 4:44
  • Can you check Max file length in the web.config file and change the "?" to a file size that you want to be your max Feb 5, 2013 at 4:53
  • 1
    Files cannot be posted asynchronously, disable ajaxform plugin and try again
    – BrunoLM
    Feb 5, 2013 at 14:32

0

Your Answer

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