0

Is it possible to transfer a file without form in a variable and without contentType: false, processData: false in ajax parameters ?

$(div).find('input, textarea').each(function(index, element) {
  //...
    var fileArr = []
    if (type === 'file' && element.files.length) {
       var file = $(element)[0].files[0];

         var fileData = new FormData();

         fileData.append('file', file);

         fileArr.push(fileData);
    }
    //...
}

    $.ajax({
                type: "POST",
                url: "/ajax/sendData",
                success: function (data) {},
                error: function (error) {},
                async: true,
                data: {
                   id: 34,
                   val: 34,
                   files: fileArr
                },
                cache: false,
                contentType: false,  //<-- without
                processData: false, //<-- without
                timeout: 10000
            });

Illegal invocation

Mr.Andrew
  • 53
  • 1
  • 6

1 Answers1

0

This is the correct syntax:

var myFormData = new FormData();
myFormData.append('pictureFile', pictureInput.files[0]);

$.ajax({
  url: 'upload.php',
  type: 'POST',
  processData: false, // important
  contentType: false, // important
  dataType : 'json',
  data: myFormData
});

source: https://stackoverflow.com/a/26690647/7641442


Anyway, as that answer says, you can avoid using an existing form by initializating a FormData() but it is needed to transfer files using ajax ^^


If you want to try a different approach you can try to convert the file to a string and pass it

Marco Salerno
  • 5,131
  • 2
  • 12
  • 32