简体   繁体   中英

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

I'm using ajax file upload javascript and php script to upload an image. It works satisfactorily with $_FILES but I need to send some additional data to the processing script. The form HTML looks like:

<form id="image1" action="" method="post" enctype="multipart/form-data">
  <label>image 1?</label>
  <p><input type="file" class="saveImage" name="image1" value="<?php echo $something; ?>" id="<?php echo $id; ?>" additional_info="some data" /></p>
  <p> <input type="submit" value="Upload" class="submit" /></p>
</form>

I need to be able to pass a variable id and some other data, call it "additional_data" to the php script, then process it in my php script using $additional_data = $_POST['additional_data'] . The javascript I'm using is:

    <script>
    $(document).ready(function (e) {
      $("#image1").on('submit',(function(e) {
        e.preventDefault();
        $("#message").empty();
        $('#loading').show();
        var DATA=$(this).val();
        var ID=$(this).attr('id');
        var ADDL=$(this).attr('additional_data');
        var dataString = 'image1='+DATA+'&id='+ID+'&additional_info='+ADDL;
        $.ajax({
          url: "uploadFile.php",  
          type: "POST",        
          // data:  new FormData(this), 
          data:  new FormData(this,dataString),
          contentType: false,   
          cache: false,   
          processData:false,        
          success: function(data)    
          {
            $('#loading').hide();
            $("#message").html(data);
          }
        });
      }));
    });
    </script>

It doesn't send the dataString, only the FILES array.

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,   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM