0

I am trying to send an image to an API to be processed, the problem is that I need to send the image captured by the pc webcam,

imageCapture.takePhoto().then(function (blob) {                                      
  var imagen = new File([blob], "name",{type:"image/jpeg"}); 
  fileUpload[0] = imagen;
});

That's how I keep the image in a variable, and so I send it to the API:

var formData = new FormData();
formData.append('files', fileUpload[0]);
    var obj = {};
    var res = "";
    var oControl = this.getView().byId("txtArea");
    $.ajax("/service/models/personas/versions/2", {
        type: "POST",
        data: formData,
        cache: false,
        headers: {
            "Authorization": "Bearer " + token
        },

But I get the following error:

Error when uploading files:: Invalid file type

I have already used it before sending an image with the same "jpeg" format with Postman and it works, so what am I doing wrong?

Hope you can help me, thanks.

Inizio
  • 2,226
  • 15
  • 18

2 Answers2

0

There is no such header called type. Instead of {type:"image/jpeg"} you need {Content-Type:"image/jpeg"} which is the appropriate HTTP header. Since postman doesn't process your data, you couldn't find the error.

Inizio
  • 2,226
  • 15
  • 18
Raymond
  • 396
  • 4
  • 21
0
            $("#btnUploadDoc").click(function (e) {
                e.preventDefault(); 
                var formData = new FormData(); 
                var opmlFile = $('#file1').get(0).files;
                formData.append(Name, opmlFile[0]); 
                $.ajax({
                    url: "/service/models/personas/versions/2",
                    type: 'POST',
                    data: formData,
                    cache: false,
                    contentType: false,
                    processData: false,
                    dataType: 'json',
                    success: function (data) {                       
                        alert(data.msg);
                    }
                });
            });
Deepak Jha
  • 359
  • 2
  • 10