提交数据而不是输入表单的文件

我正在使用jQuery来检索图像并将其发布到另一种forms:

handler = function(data, status) { ... var fd; fd = new FormData; fd.append("file", data); jQuery.ajax({ url: target_url, data: fd, processData: false, contentType: false, type: "POST", complete: function(xhr, status) { console.log(xhr.status); console.log(xhr.statusCode); } }); }; jQuery.get(imageUrl, null, handler); 

表单看起来像这样:

 
...

事情没有按预期发挥作用。 我从服务器端获得了200响应,它呈现了预先填充了一些值的表单。

我也尝试过设置contentType: "multipart/form-data"

任何想法为什么这不起作用?

您正在发送一个不会被识别为文件的字符串。 尝试发送一个blob

 fd.append("file", new Blob([data], { "type" : "text/plain" })); 

我非常确定这只适用于文本文件,除非您在原始请求上设置responseType。