javascript - jQueryAJAX - send additional data together with file upload - Stack Overflow

I am uploading files to the server using jQuery:$.ajax({url : '',dataType : 'json',

I am uploading files to the server using jQuery:

 $.ajax({
    url : '',
    dataType : 'json',
    cache : false,
    contentType : false,
    processData : false,
    data : formData, // formData is $('#file').prop('files')[0];
    type : 'post',
    success : function(response) {something}
   });

I would like to send additional parameters together with the file. Is it possible? If yes - how?

Thanks!

I am uploading files to the server using jQuery:

 $.ajax({
    url : 'http://www.example.',
    dataType : 'json',
    cache : false,
    contentType : false,
    processData : false,
    data : formData, // formData is $('#file').prop('files')[0];
    type : 'post',
    success : function(response) {something}
   });

I would like to send additional parameters together with the file. Is it possible? If yes - how?

Thanks!

Share Improve this question asked Jun 29, 2015 at 7:37 daryqsyrodaryqsyro 1552 silver badges9 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

To send additional parameters, you can just append it to formdata like below:

var formdata=new FormData();
formdata.append('simpleFile', $('#file').get('files')[0]); //use get('files')[0]
formdata.append('someotherparams',someothervalues);//you can append it to formdata with a proper parameter name 

$.ajax({
    url : 'http://www.example.',
    dataType : 'json',
    cache : false,
    contentType : false,
    processData : false,
    data : formData, //formdata will contain all the other details with a name given to parameters
    type : 'post',
    success : function(response) {something}
});

Try with this,

$( "form" ).on( "submit", function( event ) {
   var formData = $( this ).serialize();
    //$.ajax({}) //remaining code here 
});

You have to serialize the form using the FormData object instead of only sending the file.

var formData = new FormData($("form")[0]);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742319430a4421504.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信