XMLHtt prequest POST的multipart / form-data的 [英] XMLHttpRequest POST multipart/form-data

查看:755
本文介绍了XMLHtt prequest POST的multipart / form-data的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用XMLHtt prequest在JavaScript中的职位,包括文件类型输入元素的一种形式,这样我可以避开刷新页面,并获得有用的XML回来。

I want to use XMLHttpRequest in JavaScript to POST a form that includes a file type input element so that I can avoid page refresh and get useful XML back.

我可以在不刷新页面提交表单,使用JavaScript来设置窗体的iframe的MSIE或对象是Mozilla的目标属性,但有两个问题。次要问题是,目标不是W3C标准(这就是为什么我在JavaScript中设置它,而不是XHTML)。主要的问题是,onload事件不火,至少不会在Mozilla在OS X Leopard的。此外,XMLHtt prequest将使为prettier响应code,因为返回的数据可以是XML,不局限于XHTML如与IFRAME的情况。

I can submit the form without page refresh, using JavaScript to set the target attribute on the form to an iframe for MSIE or an object for Mozilla, but this has two problems. The minor problem is that target is not W3C compliant (which is why I set it in JavaScript, not in XHTML). The major problem is that the onload event doesn't fire, at least not on Mozilla on OS X Leopard. Besides, XMLHttpRequest would make for prettier response code because the returned data could be XML, not confined to XHTML as is the case with iframe.

提交表单结果在HTTP,看起来像:

Submitting the form results in HTTP that looks like:

Content-Type: multipart/form-data;boundary=<boundary string>
Content-Length: <length>
--<boundary string>
Content-Disposition: form-data, name="<input element name>"

<input element value>
--<boundary string>
Content-Disposition: form-data, name=<input element name>"; filename="<input element value>"
Content-Type: application/octet-stream

<element body>

我如何获得XMLHtt prequest对象的发送方法复制上述HTTP流?

How do I get the XMLHttpRequest object's send method to duplicate the above HTTP stream?

推荐答案

您可以构造的multipart / form-data的'要求自己(阅读更多关于它在的http://www.faqs.org/rfcs/rfc2388.html ),然后使用发送办法(即。 xhr.send(您-多部分表单数据))。同样的,但更容易,在Firefox 4+(也有镀铬5+和Safari 5+),你可以使用的 FORMDATA 界面,帮助构建这样的请求。该发送方法适用于文本内容,但如果你想二进制数据发送,如图像,你可以用 sendAsBinary <的帮助下做到这一点/ code>方法已经被各地开始与Firefox 3.0。有关如何通过 XMLHtt prequest 发送文件,请参阅<一个细节href="http://blog.igstan.ro/2009/01/pure-javascript-file-upload.html">http://blog.igstan.ro/2009/01/pure-javascript-file-upload.html.

You can construct the 'multipart/form-data' request yourself (read more about it at http://www.faqs.org/rfcs/rfc2388.html) and then use the send method (ie. xhr.send(your-multipart-form-data)). Similarly, but easier, in Firefox 4+ (also in Chrome 5+ and Safari 5+) you can use the FormData interface that helps to construct such requests. The send method is good for text content but if you want to send binary data such as images, you can do it with the help of the sendAsBinary method that has been around starting with Firefox 3.0. For details on how to send files via XMLHttpRequest, please refer to http://blog.igstan.ro/2009/01/pure-javascript-file-upload.html.

这篇关于XMLHtt prequest POST的multipart / form-data的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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