1

I have a form with file input

<input type="file" id="FileInput" multiple />

and I try to upload files to server. Server-side code like this:

[HttpPost]
public ActionResult Index(HttpPostedFileBase[] files) {
    ... process ...
}

For send post request I use $.ajax

    var formData = new FormData($('#FormId')[
    formData.append("files", $('#FileInput')[0].files);
    $.ajax({
        url: "/default/index",
        type: "POST",
        data: formData,
        contentType: false,
        processData: false,
        success: function() {
            alert("success");
        }
    });

and it doesn't work. But if I add name="files" attribute to input (for native bind) and remove instruction formData.append("files", $('#FileInput')[0].files); code works fine.

So what difference between theese ways and how I can pass files via appending data to FormData?

NOTE: in my work I can't bind form via name attribute, cause the form comes from template and have no name attribute.

  • 1
    You can just serialize your form (including file inputs) using `var formdata = new FormData($('form').get(0));` - refer [this answer](http://stackoverflow.com/questions/29293637/how-to-append-whole-set-of-model-to-formdata-and-obtain-it-in-mvc/29293681#29293681) –  Jan 27 '16 at 10:11
  • This way useless for me, cause i have no name in input. I need to append data additionaly or find the other way to pass files to server. – alexandercms Jan 27 '16 at 10:30
  • Just change the html to `` and everything will be bound correctly –  Jan 27 '16 at 10:39
  • I know and i wrote that i cant use `name` attribute for binding. I'm looking for some reason why `FormData.append` does not solve my problem. – alexandercms Jan 27 '16 at 10:44

0 Answers0