jQuery / AJAX - 将附加数据与文件上载一起发送 [英] jQuery / AJAX - send additional data together with file upload

查看:130
本文介绍了jQuery / AJAX - 将附加数据与文件上载一起发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery将文件上传到服务器:

I am uploading files to the server using jQuery:

 $.ajax({
    url : 'http://www.example.com',
    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?

谢谢!

推荐答案

要发送更多信息参数,您只需将其附加到 formdata ,如下所示:

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.com',
    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}
});

这篇关于jQuery / AJAX - 将附加数据与文件上载一起发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆