在浏览器的HTML5文件上传功能检测文件上传、浏览器、功能

2023-09-11 00:53:25 作者:感情扒手

我要上传使用HTML5功能的文件。具体根据调查我已经发现,有3个不同的载的文件的方法,

I am trying to upload the files using the HTML5 features. As per the investigation i have found that there are 3 different ways of uploading the files,

将编码文件为多部分:这样做时,有一个唯一的文件阅读器支持 发送使用XMLHTTP2(AJAX)规格方法二进制数据:新方法发(BLOB /文件)是能够通过线路发送二进制数据。 FORMDATA对象:使用 XMLHTTP(AJAX)发送(FORMDATA)方法 By encoding file as multipart: This is done when there is support for file reader only. Send binary data using XMLHTTP2(AJAX) spec method: New method send(Blob/File) is able to send the binary data across the wire. FormData object: Using XMLHTTP(AJAX) send(FormData) method.

现在的跨浏览器的问题和特征检测的片段像下面是简单的,

Now for cross browser issues and feature detections snippet like below is simple,

if(typeof FileReader == "undefined")

但我不知道如何找出如果AJAX的发送()在当前浏览器的方法支持的 发(FORMDATA)发送(BLOB /文件) 的方法实现。怎么找呢?是否有Object.property把戏吗?还是不同的东西?

However i am not sure how to find out if send() method of the AJAX in current browser is supporting send(FormData) or send(Blob/File) method implementation. How to find it ? Is there is Object.property trick here ? Or something different ?

谢谢

推荐答案

要处理的二进制数据,您将需要使用的WebSockets。这是新的 HTML5规范的一部分。这里有一个问题,虽然。截至2010年12月中旬,中,的WebSockets是因为缓存投毒漏洞禁用所有主流浏览器。

To handle binary data you will want to use WebSockets. This is part of the new HTML5 spec. There is a problem, though. As of mid-December, 2010, WebSockets were disabled in every major browser because of a cache-poisoning vulnerability.

最近我听到这仍然是整理出来。

要通过你应该使用的WebSockets的的FileReader API 发送之前上传的文件,该文件支持在每个浏览器的最新版本(尽我所知)。

To upload the file before sending it via WebSockets you should use the FileReader API which is supported in the latest version of each browser (to the best of my knowledge).

要检查是否支持的FileReader你应该测试这样的:

To check if the FileReader is supported you should test like:

if (FileReader){
  // It's supported
}

您还可以检查:

if (window.URL){
  //
}

一种替代。