简体   繁体   English

用于Internet Explorer的后备AJAX文件上传

[英]Fallback AJAX file upload for Internet Explorer

I have implemented an AJAX file uploader in HTML5 using xHR2 and File api for an internal project. 我已经针对内部项目使用xHR2和File api在HTML5中实现了AJAX文件上传器。 We were only required to support Firefox/Safari/Chrome. 我们只需要支持Firefox / Safari / Chrome。 I used the following links as a reference 我使用以下链接作为参考

  1. http://www.html5rocks.com/en/tutorials/file/xhr2/ http://www.html5rocks.com/zh-CN/tutorials/file/xhr2/
  2. http://www.html5rocks.com/en/tutorials/dnd/basics/ http://www.html5rocks.com/zh-CN/tutorials/dnd/basics/
  3. https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications https://developer.mozilla.org/zh-CN/docs/Using_files_from_web_applications

Now I am required to support Internet Explorer 10 and below. 现在,我需要支持Internet Explorer 10及更低版本。 IE 10 supports XHR2 but File API support is lacking . IE 10支持XHR2,但缺少文件API支持。 Is there a way to detect that these APIs are not supported? 有没有办法检测不支持这些API? If I can detect that the APIs are not supported, then how do I implement AJAX file upload for IE? 如果可以检测到不支持这些API,那么如何实现IE的AJAX文件上传?

I am using Google Closure JS library not jQuery or any other library. 我正在使用Google Closure JS库而不是jQuery或任何其他库。 The choice of library cannot be changed. 库的选择无法更改。 Please use jQuery or some ones to that effect in the response. 请在响应中使用jQuery或其他实现该效果的工具。 However, any code snippets that use jQuery are perfectly alright. 但是,使用jQuery的任何代码段都可以。

probably the easiest way to detect if the File API exists is to 检测File API是否存在的最简单方法可能是

typeof File

In a browser that supports the File API it should return an object or function - Internet Explorer returns undefined 在支持File API的浏览器中,它应该返回一个对象或函数-Internet Explorer返回undefined

as far as how to fallback and make it work for all users 关于如何回退并使其适用于所有用户

I've used this tool https://github.com/valums/ajax-upload that creates an iframe and posts the upload to the iframe and emulates an AJAX file upload - its all pure javascript no libraries required and supports all the way back to IE6 我使用了此工具https://github.com/valums/ajax-upload ,该工具创建了一个iframe,并将上传内容发布到iframe中,并模拟了AJAX文件上传-其所有纯javascript不需要库,并且一直支持到IE6

Recently I've been working on a file uploader to be used together with google closure library. 最近,我一直在致力于与Google闭包库一起使用的文件上传器。 I found the fine-uploader library a good start. 我发现优良上传器库是一个好的开始。 It's not written with the closure library, but it provides me the basic guidelines: uses xhr to upload a file if supported, otherwise fallback to form post into a hidden iframe. 它不是用闭包库编写的,但是它为我提供了基本准则:使用xhr上载文件(如果支持),否则使用后备表单将其发布到隐藏的iframe中。

In my code, I implemented two classes: one uses a XMLHttpRequest to upload a file, and the other use goog.net.IframeIo 's sendFromForm method to upload a file. 在我的代码中,我实现了两个类:一个使用XMLHttpRequest上传文件,另一个使用goog.net.IframeIosendFromForm方法上传文件。

Take a look at the goog.net.iframeIo class in the Google Closure Library. 看看Google Closure库中的goog.net.iframeIo类。 This will take a file input element from a form and post it to an iFrame, giving the equivalent of an AJAX experience, even if it isn't in an actual XHR. 这将从表单中获取文件输入元素,并将其发布到iFrame,即使在实际的XHR中也不会有AJAX体验。

Here's a link to Google's documentation on how to implement it: 以下是有关如何实现Google文档的链接:

https://code.google.com/p/closure-library/source/browse/closure/goog/net/iframeio.js#48 https://code.google.com/p/closure-library/source/browse/closure/goog/net/iframeio.js#48

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM