Is it possible to add file in api call form data?

时间:2017-08-05 10:32:51

标签: javascript jquery

I want to pass file into api call. I already have a file location.

var formData = new FormData();

var firstName='xyz' , lastName = '123', emailID="xyz@gmail.com",
doc = '/User/Desktop.jpg'; //This is a filename of an image

formData.append("first_name",firstName);
formData.append("last_name",lastName);
formData.append("email",emailID);
formData.append("document",doc);  // here i got error

$.ajax({
        url: apiurl,
        type:'POST',
        data:formData,
        success: function (data) {

            if(data.approved==true)
               alert('success');

            },
        error:function(result){
               alert(result.responseText)
            },
        cache: false,
        contentType: false,
        processData: false
    });

It returns an error. Because, i pass doc type as String. I want to change the doc type as file.

1 个答案:

答案 0 :(得分:0)

Like Suresh Atta mentioned, you can convert the image to a blob and send.

See if this helps. https://www.html5rocks.com/en/tutorials/file/dndfiles/

Contains a nice tutorial on using the File API.