Skip to content
Advertisement

How to handle posted multipart/form-data when its send to PHP?

Rather simple question but I’m new to handling formdata and blobs. I need to handle a formdata file that contains both a blob and a string. The blob is first downloaded and then sent successfully to testPDF however in doing a var_dump of $_FILE[‘pdf_blob’] or $_POST[‘pdf_blob’] it comes out null, so probably I’m doing something wrong. Below the relevant ajax block

        var pdf_blob = new FormData();
        pdf_blob.append(filename, my_blob);
        pdf_blob.append('string', global_hash);

        $.ajax({
            url :  "testPDF.php",
            type: 'POST',
            data: pdf_blob,
            contentType: false,
            processData: false,
            success: function(data) {
            },    
            error: function() {
              alert("error");
            }
        }); 

Logging in console shows me that pdf_blob is not empty before it is sent through ajax

console.log(pdf_blob.get(filename));
console.log(pdf_blob.get('string'));

Advertisement

Answer

Solved by adding a name to the field separate from the blob’s filename, like so: Javascript

pdf_blob.append('blob', my_blob, filename);

then on PHP

var_dump($_FILE['blob']);

var_dumping $_FILE directly or $_FILE[0] will always be empty

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement