How do I add additional POST parameters to ajax file upload? How do I add additional POST parameters to ajax file upload? php php

How do I add additional POST parameters to ajax file upload?


I also wanted to do the same thing. Here's my solution :

The JS part :

var file_data = this.files[0];    file_data.name = idaviz +'.pdf';    var form_data = new FormData();    form_data.append("file", file_data);    form_data.append('extraParam','value231');    console.log(file_data);    console.log('here');    var oReq = new XMLHttpRequest();    oReq.open("POST", "ajax_page.php", true);    oReq.onload = function (oEvent) {        if (oReq.status === 200) {            console.log('upload succes',oReq.responseText);        } else {            console.log("Error " + oReq.status + " occurred when trying to upload your file.<br \/>");        }    };    oReq.send(form_data);});

The PHP part:

echo $_REQUEST['extraParam']; //this will display "value231"var_dump($_FILES['file']);   //this will display the file object

Hope it helps.

Addition info about extra parameters on formData can be found here!


I hope I understand you right. Maybe this snippet helps you:

var formData = new FormData();formData.append("image1", fileInputElement.files[0]);formData.append("ID", ID);formData.append("ADDL", ADDL);

And then set this formData variable as data:

type: "POST",        data:  formData,contentType: false,