0

I am using .net mvc and are uploading files to

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

.net mvc puts the files nicely into that variable and it works with both one and multiple files.

But here is the odd thing, if I have 3 inputs and I leave one empty then chrome will send this:

------WebKitFormBoundaryU3zJOb5diOM9JOaS
Content-Disposition: form-data; name="SubmissionUploadFiles"; filename="Glidecam.jpg"
Content-Type: image/jpeg


------WebKitFormBoundaryU3zJOb5diOM9JOaS
Content-Disposition: form-data; name="SubmissionUploadFiles"; filename="fire.svg"
Content-Type: image/svg+xml


------WebKitFormBoundaryU3zJOb5diOM9JOaS
Content-Disposition: form-data; name="SubmissionUploadFiles"

As you see the last one has no value (as I did not select a file in that input!

Here is a screenshot showing graphically my 3 inputs Last one is empty

When one is empty like this the following happens in .net mvc i get one null file like so Nothing gets in

However if I select 3 files (or add more - i add inputs with jquery) then it works great as you can see here.

This way it works


I change nothing it's only if I have an empty file input or not. I can of course with jquery on submit remove empty fields as a workaround but i just think it's odd and would like a better solution.

Rickard Liljeberg
  • 966
  • 1
  • 12
  • 39
  • You should only send as many files as can be sent. If the user has selected no files, no files should be sent. If only one file was selected, only one file should be sent. So what you can do (can't tell from your code how you're posting), but you can dynamically create the input fields for the files, handle their onchange events with jQuery. When submit is clicked, use a POST type Ajax request to send only the selected files to your action in a FormData JavaScript object. Then in the action, check if Request.IsAjaxRequest and use the Request.Files collection for whatever you're doing. – scottyeatscode Jul 15 '15 at 06:19
  • Well as i said a workaround would be to filter away on client side but still does not feel like an amazing solution, for instance it requires javascript. Secondly I would like to use the MVC framework to put files into my variables sent in. Using Request.Files breaks the idea of MVC. So yes maybe I can solve it but i am wondering if there is a way more in line with mvc ideals. – Rickard Liljeberg Jul 15 '15 at 12:07

0 Answers0