ITsolutionWizard
asked on
mvc, ajax upload with data
https://stackoverflow.com/questions/32470906/how-to-upload-a-image-in-asp-net-mvc-using-ajax-request
I use above codes to upload a file. but if I want to add a parameter from <input type="text" id="firstname"> to ajax like below. How can I do that? Basically, I upload the image but I also want to update the database behind.
type: "POST",
url: '/Home/Upload',
data: formData, document.getelementid("fir stname").v alue,
dataType: 'json',
contentType: false,
I use above codes to upload a file. but if I want to add a parameter from <input type="text" id="firstname"> to ajax like below. How can I do that? Basically, I upload the image but I also want to update the database behind.
type: "POST",
url: '/Home/Upload',
data: formData, document.getelementid("fir
dataType: 'json',
contentType: false,
append the field to the formData object before the ajax call:
...
for (var i = 0; i < totalFiles; i++) {
var file = document.getElementById("imageUploadForm").files[i];
formData.append("imageUploadForm", file);
}
formData.append("firstname", $("#firstname").val() );
$.ajax({
type: "POST",
url: '/Home/Upload',
data: formData,
dataType: 'json',
contentType: false,
processData: false
}).done(function(data) {
alert('success');
console.log(data);
}).fail(function( xhr, status, errorThrown ) {
alert('fail');
});
ASKER
thanks but how to call each parameter in the mvc controller?
I am not a .Net developer, however, I did some searching and here's what I found. You can retrieve the files using:
s/upload-f iles-using -ajax-in-a sp-net-mvc /
As for the non-file data, try retrieving the values from Request.Form:
HttpPostedFileBase file = Request.Files[i];
see http://ajeeshms.in/articleAs for the non-file data, try retrieving the values from Request.Form:
Request.Form("firstname")
ASKER
that won't work to me because it accepts more than 1 files from the upload.
how to append upload file just like the following?
formData.append("firstname ", $("#firstname").val() );
how to append upload file just like the following?
formData.append("firstname
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
how to call my file on mvc controller?
ASKER