gpt4 book ai didi

javascript - jQuery/AJAX - 与文件上传一起发送附加数据

转载 作者:行者123 更新时间:2023-11-30 00:26:50 27 4
gpt4 key购买 nike

我正在使用 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}
});

我想将附加参数与文件一起发送。是否可以?如果是 - 如何?

谢谢!

最佳答案

要发送额外的参数,您只需将其附加到 formdata 中,如下所示:

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}
});

关于javascript - jQuery/AJAX - 与文件上传一起发送附加数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31110207/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com