I have a array in jquery. And i have a file list in view.
I want to put this file list into my array and pass my controller/action
I find some ways but dont work.
there is my jquery
var Data = new FormData();
$('input[data-fileid=1]').each(function () {
var FileData = $(this).get(0).files[0];
Data.append("Attachments", FileData);
});
var arr = {
Code: "examplecode",
Id: 1
FileList: Data
};
$.ajax({
data: JSON.stringify(arr),
url: '/Panel/PD',
datatype: 'json',
type: 'POST',
contentType: 'application/json; charset=utf-8',
});```
and I don't know how to keep this data in class.
2
Answers
So your ajax should send data that required in controller,
Controlller will look like:
When sending binary file data in an AJAX request you cannot send data as a serialised string. It needs to be contained within a
FormData
object.From there, you need to set the
contentType
andprocessData
tofalse
within the$.ajax()
settings so that jQuery doesn’t attempt to change the request content or the content-type header. Try this:On the server side you can use the
Request.Files
collection to iterate through the file data in the request as normal – assuming you haven’t extended the default ModelBinder to receive binary data.Also note that a simpler way to build the
FormData
object is to pass a reference to theform
element to the constructor. All data within the controls of the form will then be appended to the object. If you want to append custom data to this, use hiddeninput
controls within the HTML.This avoids the need to manually
append()
any magic strings in your JS logic.