提交数据而不是输入表单的文件 [英] Submitting data instead of a file for an input form

查看:52
本文介绍了提交数据而不是输入表单的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using jQuery to retrieve an image and post it to another form:

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

表格如下所示:

<form>
  <input type="file" name="file" />
  ...
</form>

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

Things aren't working as expected. I'm getting a 200 response from the server side, and it renders the forms with some of the values pre-populated.

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

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

Any ideas why this is not working?

推荐答案

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

You're sending a string which will not be recognized as a file. Try sending a blob

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

我很确定这只适用于文本文件,除非你在原版上设置responseType请求。

I'm pretty sure this will only work for text files, unless you set the responseType on the original request.

这篇关于提交数据而不是输入表单的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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