xhr.send(文件)不会将其作为多部分发布 [英] xhr.send(file) doesn't post it as multipart

查看:249
本文介绍了xhr.send(文件)不会将其作为多部分发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Firefox 3.6和Chrome上,使用xhr.send(file)只是将原始内容放到请求的主体中,而不是真正的多部分/表单数据上传。

试过这样做: http:// kaply.com/weblog/2010/05/20/post-multipart-form-xhr/ b
$ b

但是,不能真正将字符串与文件内容混合发送()期间。

任何解决方法?

解决方案 .sendAsBinary()是非标准的。而是使用 xhr.send(FormData),它会创建一个 multipart / form-data 请求,允许附加文件,和任意形式的数据。

  var formData = new FormData(); 
formData.append(file.name,file);

var xhr = new XMLHttpRequest();
xhr.open('POST','/ upload',true);
xhr.onload = function(e){...};

xhr.send(formData); // multipart / form-data

请参阅 http://www.html5rocks.com/en/tutorials/file/xhr2/#toc-send-formdata


On Firefox 3.6 and Chrome, using xhr.send(file) just puts the raw contents into the body of the request and it is not a true multipart/form-data upload.

Tried doing this: http://kaply.com/weblog/2010/05/20/post-multipart-form-xhr/

But, can't really mix string with File contents during send().

Any workarounds?

解决方案

xhr.sendAsBinary() is non-standard. Instead, use xhr.send(FormData), which does create a multipart/form-data request, allows appending files, and arbitrary form data.

var formData = new FormData();
formData.append(file.name, file);

var xhr = new XMLHttpRequest();
xhr.open('POST', '/upload', true);
xhr.onload = function(e) { ... };

xhr.send(formData);  // multipart/form-data

See http://www.html5rocks.com/en/tutorials/file/xhr2/#toc-send-formdata

这篇关于xhr.send(文件)不会将其作为多部分发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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