Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught TypeError: Failed to execute 'append' on 'FormData': 2 arguments required, but only 1 present

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);
        }
    });
});
like image 280
Salis Tariq Avatar asked Jan 28 '17 18:01

Salis Tariq


2 Answers

Easy's answer is right, if you don't understand his answer there is what you want.

formData.append('chooseAName', $('#imageFile')[0].files[0]);
like image 62
Etienne Avatar answered Nov 14 '22 23:11

Etienne


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

like image 35
easy Avatar answered Nov 14 '22 23:11

easy