I am working on MVC 5 application. And i need to upload multiple files.
What I know is HttpPostedFile and HttpPostedFileBase classes can get one file. But my condition is I need multiple files to be uploaded at once.
My question is,
1) Since there is no support for multiple file upload using ajax, do I need to write value provider that make my action to accept multiple files?
2) If I implement custom value provider, what should I use for parameter in action method (should it be IEnumerable<HttpPostedFileBase> f
)?Because I did that and I am getting null
.
Update
Here is my Ajax call from View
var files = e.target.files;
if (window.FormData !== undefined) {
var fd = new FormData();
for (x = 0; x < files.length; x++) {
fd.append("file" + x, files[x]);
}
// fd.append("fawad", "ali");
$.ajax({
type: "POST",
url: "/FileOp/FileUpload",
contentType: false,
processData: false,
data: fd,
sucess: function (result) {
// alert();
},
error: function (xhr, status, p3, p4) {
alert(xhr.responseText);
}
});
And here is my action method (HttpPost)
[HttpPost]
public object FileUpload(IEnumerable<HttpPostedFileBase> file)
Thanks
Your add files named file0
, file1
, file2
etc which will not bind to a parameter named file
.
Change the code in the script to
for (x = 0; x < files.length; x++) {
fd.append("file", files[x]); // modify
}
Alternatively, you could use
fd.append("[" + x + "].file", files[x]);