Reputation: 216
I am able to pass single file as System.Web.HttpPostedFileBase but when i pass same as array of files i am getting null in controller's action.
I've tried sending array of files.
HTML:
<input type="file" id="Attachment1">
<input type="file" id="Attachment2">
<input type="file" id="Attachment3">
<input type="file" id="Attachment4">
<input type="file" id="Attachment5">
Javascript:
var FileData = [];
$('input').each(function () {
var type = $(this).attr("type");
if (type == "file") {
FileData.push($(this).get(0).files[0]);
}
});
var Data = new FormData();
Data.append("Attachments", FileData);
if (url != '') {
$.ajax({
url: url,
data: Data,
type: "POST",
contentType: false,
processData: false,
success: function (data) {
alert("Saved successfully");
}
});
}
Controller:
public ActionResult InsertDetails(System.Web.HttpPostedFileBase[] Attachments)
{
return Json(new { Success = false });
}
Need to get array of files. Thanks in advance.
Upvotes: 2
Views: 3462
Reputation: 65
This is how I used to post array data from javascript to mvc controller, this is going to be a little bit lengthy but I've explained each line with comment, I hope this may help you
javascript:
var formData = new FormData(); //declare formData
var arrayData = []; //declare array and push/append your data to it.
var name="abc";
arrayData.push(name);
//setting ArrayData to Json Object
var AllData = {
getUserData: arrayData
};
//appending Json Object to formdata with the key "mydata"
formData.append("mydata", JSON.stringify(AllData));
//sending formdata through ajax request
$.ajax({
type: "POST",
url: yourURLHere,
processData: false,
contentType: false,
data: formData,
cache: false,
success: function (data) {
//your program logic here
}
});
Controller:
public async Task<HttpResponseMessage> SaveResponse()
{
//receiving json data from the key "mydata" we set earlier
var getData = HttpContext.Current.Request.Params["mydata"];
//deserialize json object (you will need Newtonsoft.Json library)
var model = JsonConvert.DeserializeObject<MyModel>(getData);
//you will get all of your data in model variable
//do what you want to do with that data
}
and here is the model class
public class MyModel
{
//**dataList** will receive array data sent from javascript
public List<MyModel> dataList = new List<MyModel>();
//Remember, whatever your push to array in javascript, should be declared here.
public string name {get;set;}
}
Upvotes: 0
Reputation: 216
Thanks for the effort guys. I found the solution. I just need to keep append files for same key "Attachments". Now i am able to get as array of HttpPostedFileBase.
var Data = new FormData();
$('input').each(function () {
var type = $(this).attr("type");
if (type == "file") {
var FileData = $(this).get(0).files[0]);
Data.append("Attachments", FileData);
}
});
Upvotes: 2
Reputation: 559
Try It:
var Files = new FormData();
$('input').each(function () {
var type = $(this).attr("type");
if (type == "file") {
Files .append("Attachment"+$(this).attr("id"), $(this).get(0).files[0]);
}
});
if (url != '') {
$.ajax({
url: url,
data: Files ,
type: "POST",
contentType: false,
processData: false,
success: function (data) {
alert("Saved successfully");
}
});
}
Upvotes: 0