I am sending the form data to web api controller in asp.net mvc but my ajax request not hitting the controller every time i got the above error kindly tell me how to send request with files in ajax in asp.net mvc to web api controller
$(document).on("submit", ".SignupForm", function (event) {
event.preventDefault();
var formData = new FormData($(".SignupForm"));
formData.append($('#imageFile')[0].files[0]);
$.ajax({
url: 'api/Countries',
type:'POST',
data: obj,
dataType: 'json',
processData: false,
success: function (result) {
console.log(result);
},
error: function (data, status, abc) {
console.log(data);
console.log(status);
console.log(abc);
}
});
});
Easy's answer is right, if you don't understand his answer there is what you want.
formData.append('chooseAName', $('#imageFile')[0].files[0]);
See this: (apparently you must add argument)
SyntaxEDIT There are two versions of this method: a two and a three parameter version:
formData.append(name, value); formData.append(name, value, filename); Parameters
name The name of the field whose data is contained in value. value The field's value. This can be a USVString or Blob (including subclasses such as File). filename Optional The filename reported to the server (a USVString), when a Blob or File is passed as the second parameter. The default filename for Blob objects is "blob". The default filename for File objects is the file's filename. ExampleEDIT The following line creates an empty FormData object:
var formData = new FormData(); // Currently empty You can add key/value pairs to this using FormData.append:
formData.append('username', 'Chris'); formData.append('userpic', myFileInput.files[0], 'chris.jpg'); As with regular form data, you can append multiple values with the same name. For example (and being compatible with PHP's naming conventions by adding [] to the name):
formData.append('userpic[]', myFileInput1.files[0], 'chris1.jpg'); formData.append('userpic[]', myFileInput2.files[0], 'chris2.jpg'); This technique makes it simpler to process multi-file uploads because the resultant data structure is more conducive to looping.
https://developer.mozilla.org/en-US/docs/Web/API/FormData/append
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With