struggling my head trying to pass a file and parameters from javascript using AJAX to mvc method.
I already successfully send and catch the file, but I dont know have to send and receive the strings parameters.
Javascript:
$('#importButton').on("change", function () {
var fileUpload = $("#importButton").get(0);
var files = fileUpload.files;
var systemName = "param1";
var clientName = "param2";
// Create FormData object
var fileData = new FormData();
// Looping over all files and add it to FormData object
for (var i = 0; i < files.length; i++) {
fileData.append(files[i].name, files[i]);
}
$.ajax({
url: ImportClick,
type: 'POST',
//here is the thing, how send the parameters and the file
data: fileData,
cache: false,
processData: false,
contentType: false,
success: function (response) {
alert(response);
},
error: function (err) {
alert(err.statusText);
}
});
MVC:
[HttpPost]
public ActionResult ImportClick()
{
//how take the two parameters??
//Here take the file
HttpFileCollectionBase files = Request.Files;
byte[] fileData = null;
using (var binaryReader = new BinaryReader(files[0].InputStream))
{
fileData = binaryReader.ReadBytes(files[0].ContentLength);
}
service.ImportAllClientConfigurationData(param1, param2, fileData);
}