热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

创建在IE中工作的文件上传-createfileuploadthatworkinIE

IwanttowriteafileuploadscriptthatworksinIEbutthetwotypesofcodethatImwritinghav

I want to write a file upload script that works in IE but the two types of code that I'm writing have problems in IE.

我想写一个可以在IE中工作的文件上传脚本,但是我写的两种代码在IE中有问题。

Please help. How can you write a file upload script that works in IE?

请帮助。如何编写在IE中工作的文件上传脚本?

Type 1
Problem Not Support File Api In IE (Is the trick not to use it?)

第1类问题不支持IE中的文件Api(不使用它吗?)

    



    
    
    


    
    

selected files: 0; total size: 0

Type 2
Problem Not Support document.getElementById('fileToUpload').files[0](Is the trick not to Get Files[0]?)

类型2问题不支持document.getElementById('fileToUpload')。文件[0](不获取文件的诀窍是[0]吗?)

 
    



    
    

Please Help :(

请帮助:

1 个解决方案

#1


51  

You can't use these functions unless you're using IE10 or another modern browser. Workarounds are possible for earlier versions of Internet Explorer (and other browsers), but you'll need to adjust your back-end code too.

除非使用IE10或其他现代浏览器,否则无法使用这些功能。对于早期版本的Internet Explorer(和其他浏览器)来说,解决方案是可能的,但是您也需要调整后端代码。

Why it doesn't work

Internet Explorer up until version 10 doesn't support a number of these features, the key ones being the FormData and FileReader APIs. Both of your code snippets rely on the FileReader API, and the second one also relies on FormData to upload the file dynamically.

Internet Explorer直到第10版都不支持这些特性,关键的特性是FormData和FileReader api。您的两个代码片段都依赖于FileReader API,第二个代码片段还依赖于FormData来动态上传文件。

How to determine whether to execute the code or not

I recently wrote a file upload widget that detected these features and served different code depending on support. I used the feature detections from Modernizr, because it's tests are regularly put to the test by the open source community:

我最近编写了一个文件上传小部件,它检测了这些特性,并根据支持提供不同的代码。我使用了来自Modernizr的特性检测,因为它的测试经常被开源社区进行测试:

var support  = {
  // Are files exposed to JS?
  // As used by Modernizr @
  // https://github.com/Modernizr/Modernizr/blob/master/feature-detects/file/api.js
  'fileReader'  : (function testFileReader(){
    // Test: look for global file class.
    return !!(window.File && window.FileList && window.FileReader);
  }()),

  // AJAX file upload via formData?
  'formData'    : window.FormData !== void 0
};

For your fileSelected function, you'll need support.fileReader to evaluate true; for uploadFile, you need support.formData.

对于您的文件选择函数,您将需要支持。fileReader评估正确;对于uploadFile,需要support.formData。

A workaround for browsers that don't support these features

Without these features it's impossible to read a file from the front-end or to send a file using AJAX. What you can do, though, is send your file via a hidden 发送文件,并获取UploadHandler。ashx以不同的方式响应非xhr请求。

This solution is technically synchronous (just happening in another, hidden page), so you won't get updates — the only feedback is when the upload is complete and the server has responded. So you will only be able to inform the user as to file name, size, and success once they have completely uploaded it — which might take a while!

这个解决方案在技术上是同步的(只是发生在另一个隐藏的页面中),所以你不会得到更新——唯一的反馈是上传完成后,服务器响应了。所以你只需要告诉用户文件的名称,大小,和成功一旦他们完全上传了它-这可能需要一段时间!

Anyway, the HTML for this would look as follows:

不管怎样,这个HTML看起来是这样的:


  
  

selected files: 0 ; total size: 0

A few changes:

一些变化:

  1. The form now has a target, which means that when it posts its content to the URI in action, it will load the response in there, as opposed to on the current page.
  2. 表单现在有了一个目标,这意味着当它将其内容发布到活动的URI时,它将在那里加载响应,而不是在当前页面上。
  3. The target is a name reference to an iframe we've included. It is hidden with display:none, and given a negative tabindex just to make sure the user doesn't stumble into it. It also has an onload property specified. This is the only way of binding functions to the load event in older versions of IE.
  4. 目标是我们包含的iframe的名称引用。它隐藏在display:none中,并给出一个负值的tabindex,以确保用户不会陷入其中。它还指定了onload属性。这是将函数绑定到旧版本IE中的load事件的惟一方法。

So when the form is submitted, we stay on the current page and the server response loads in our hidden iframe. When that happens, the browser will execute the function named in the onload attribute. Sadly, this means that function needs to be in the global scope!

因此,当表单提交时,我们停留在当前页面上,服务器响应在隐藏的iframe中加载。当发生这种情况时,浏览器将执行onload属性中命名的函数。遗憾的是,这意味着该函数需要在全局范围内!

Back-end stuff

I don't know how your back-end works, but if the iframe is to load the response instead of downloading it, it will need to be HTML or plain text (and that will need to be specified in the mime-type). You can tell whether the form was posted via AJAX from the back-end by looking for the X-Requested-With header, which should have a value of XMLHttpRequest — if that isn't there, then the iframe is asking for the response and you need to send text or HTML. You may want to stringify a JSON response that exposes the values you wanted to feed back to the user like fileName, fileSize & fileType. I'm hoping you can do this yourself or get a colleague to handle it.

我不知道您的后端是如何工作的,但是如果iframe要加载响应而不是下载它,那么它需要是HTML或纯文本(这需要在mime类型中指定)。您可以通过查找X-Requested-With报头(它应该有XMLHttpRequest的值)来从后端判断表单是通过AJAX发布的,如果没有,那么iframe请求响应,您需要发送文本或HTML。您可能希望对JSON响应进行字符串化,该响应公开您想要反馈给用户的值,如文件名、fileSize和fileType。我希望你能自己做这件事,或者找个同事来处理。

Capturing the iframe response

As mentioned, the response handler function will need to be in the global scope for the onload attribute to bind to, because of old IE being very quirky. I see you're using jQuery, so if you went down the route of stringifying the server response, you could write this function as follows:

如前所述,响应处理程序函数需要在onload属性的全局范围内绑定,因为老IE非常古怪。我看到您正在使用jQuery,所以如果您沿着字符串化服务器响应的路线,您可以编写如下函数:

function parseIframeResponse(){
  var respOnse= $('#fileIframe').contents().find('body').text();
  var object   = $.parseJSON(response);
}

Issues with the iframe load event binding

iframe负载事件绑定问题

As mentioned earlier, the iframe load event needs to be bound inline as an attribute of the iframe itself — this is because IE will simply fail to register it otherwise. But this is problematic in and of itself because even an empty iframe (an empty or non-present src will default to about:blank) fires a load event. To mitigate this you will need to discard any response that evaluates to an empty string as a false positive, and make sure your back-end responds with some content even if it encounters a fault.

如前所述,iframe load事件需要作为iframe本身的一个属性内联地绑定—这是因为IE将无法注册它。但这本身就有问题,因为即使是空的iframe(一个空的或不存在的src也会默认为:blank)触发一个加载事件。为了减轻这一点,您将需要丢弃任何对空字符串计算为假阳性的响应,并确保您的后端对某些内容进行响应,即使它遇到了错误。

Presumably, you would then want to use whatever information is in there to execute some of the code you've currently got in the functions fileSelected, uploadProgress, etc.

然后,您可能会想要使用那里的任何信息来执行您当前在函数文件、uploadProgress等功能中获得的一些代码。

Hope this helps.

希望这个有帮助。

EDIT 1: Out-of-the-box solutions

In hindsight, despite writing this off the back of having developed my own solution to the problem, it could be considered negligent not to mention Fine Uploader, a heavily tested (over 700 closed issues!) and well maintained stand-alone plugin that aims to achieve the best file upload experience possible for IE7 and up. There's also a good selection of back-end server components — including ASP.NET — to parse the upload. You might find it easier to tweak this than roll your own!

事后看来,尽管写这后面的开发自己的解决方案,它可以被认为是过失更不用说好上传,大量测试(在700年关闭问题!),保持独立的插件,旨在实现最佳文件上传可能IE7和经验。还有很好的后端服务器组件选择——包括ASP。NET -解析上传。你可能会发现调整这个比自己动手更容易!

EDIT 2: Neglected to mention issues with iframe's load event. Amended answer in place.


推荐阅读
  • Nginx使用(server参数配置)
    本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 1,关于死锁的理解死锁,我们可以简单的理解为是两个线程同时使用同一资源,两个线程又得不到相应的资源而造成永无相互等待的情况。 2,模拟死锁背景介绍:我们创建一个朋友 ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • ASP.NET2.0数据教程之十四:使用FormView的模板
    本文介绍了在ASP.NET 2.0中使用FormView控件来实现自定义的显示外观,与GridView和DetailsView不同,FormView使用模板来呈现,可以实现不规则的外观呈现。同时还介绍了TemplateField的用法和FormView与DetailsView的区别。 ... [详细]
  • 本文介绍了如何使用C#制作Java+Mysql+Tomcat环境安装程序,实现一键式安装。通过将JDK、Mysql、Tomcat三者制作成一个安装包,解决了客户在安装软件时的复杂配置和繁琐问题,便于管理软件版本和系统集成。具体步骤包括配置JDK环境变量和安装Mysql服务,其中使用了MySQL Server 5.5社区版和my.ini文件。安装方法为通过命令行将目录转到mysql的bin目录下,执行mysqld --install MySQL5命令。 ... [详细]
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • 本文介绍了使用AJAX的POST请求实现数据修改功能的方法。通过ajax-post技术,可以实现在输入某个id后,通过ajax技术调用post.jsp修改具有该id记录的姓名的值。文章还提到了AJAX的概念和作用,以及使用async参数和open()方法的注意事项。同时强调了不推荐使用async=false的情况,并解释了JavaScript等待服务器响应的机制。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • t-io 2.0.0发布-法网天眼第一版的回顾和更新说明
    本文回顾了t-io 1.x版本的工程结构和性能数据,并介绍了t-io在码云上的成绩和用户反馈。同时,还提到了@openSeLi同学发布的t-io 30W长连接并发压力测试报告。最后,详细介绍了t-io 2.0.0版本的更新内容,包括更简洁的使用方式和内置的httpsession功能。 ... [详细]
  • 本文介绍了使用PHP实现断点续传乱序合并文件的方法和源码。由于网络原因,文件需要分割成多个部分发送,因此无法按顺序接收。文章中提供了merge2.php的源码,通过使用shuffle函数打乱文件读取顺序,实现了乱序合并文件的功能。同时,还介绍了filesize、glob、unlink、fopen等相关函数的使用。阅读本文可以了解如何使用PHP实现断点续传乱序合并文件的具体步骤。 ... [详细]
  • 本文讨论了在Spring 3.1中,数据源未能自动连接到@Configuration类的错误原因,并提供了解决方法。作者发现了错误的原因,并在代码中手动定义了PersistenceAnnotationBeanPostProcessor。作者删除了该定义后,问题得到解决。此外,作者还指出了默认的PersistenceAnnotationBeanPostProcessor的注册方式,并提供了自定义该bean定义的方法。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • Voicewo在线语音识别转换jQuery插件的特点和示例
    本文介绍了一款名为Voicewo的在线语音识别转换jQuery插件,该插件具有快速、架构、风格、扩展和兼容等特点,适合在互联网应用中使用。同时还提供了一个快速示例供开发人员参考。 ... [详细]
  • 本文介绍了使用cacti监控mssql 2005运行资源情况的操作步骤,包括安装必要的工具和驱动,测试mssql的连接,配置监控脚本等。通过php连接mssql来获取SQL 2005性能计算器的值,实现对mssql的监控。详细的操作步骤和代码请参考附件。 ... [详细]
author-avatar
一个有点自卑的小孩
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有