Javascript IE 和本地文件读取

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6710432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:52:58  来源:igfitidea点击:

IE and local file reading

javascriptinternet-explorerfileapi

提问by user592704

I've just watched the mozilla File API file reading as

我刚刚看了 mozilla File API 文件读取为

new FileReader();

etc. and I must ask is there something like that for IE?

等等,我必须问 IE 是否有类似的东西?

回答by duri

Yes, you can use ActiveX' FileSystemObject. However, an confirmation box is shown to the user everytime he runs the code. Some users might not trust you and could choose not to run the ActiveX control. Also, please note that some users also use non-IE browsers which don't support FileReader (Safari, older versions of Firefox and so on). By adding ActiveX, you still won't have 100% support for file-related APIs.

是的,您可以使用ActiveX 的 FileSystemObject。但是,每次运行代码时,都会向用户显示一个确认框。某些用户可能不信任您,并可能选择不运行 ActiveX 控件。另外请注意,有些用户还使用不支持 FileReader 的非 IE 浏览器(Safari、旧版本的 Firefox 等)。通过添加 ActiveX,您仍然无法 100% 支持与文件相关的 API。

回答by Sampson

Internet Explorer 10 also supports the FileReader:

Internet Explorer 10 还支持FileReader

var reader = new FileReader();
reader.onloadend = function(){
    // do something with this.result
}
reader.readAsText(readFile);

For managed compatability tables regarding the FileReader, be sure to check out caniuse.com.

有关 FileReader 的托管兼容性表,请务必查看caniuse.com

If you wanted to provide a fall-back for those who may not be visiting your site in Internet Explorer 10, I would encourage you to do a bit of feature-detection to determine whether or not you want to use the FileReader:

如果您想为那些可能不会在 Internet Explorer 10 中访问您的站点的人提供后备,我建议您进行一些功能检测以确定您是否要使用 FileReader:

if ( window.FileReader ) {
    /* Use the FileReader */
} else {
    /* Do something else */ 
}

Note also that using an ActiveXObject approach isn't necessarily going to work all the time either as some users browse with ActiveX Filteringenabled, meaning you can't touch their file-system, or run any types of plugins in their browser.

另请注意,使用 ActiveXObject 方法不一定始终有效,因为某些用户在启用ActiveX 过滤的情况下浏览,这意味着您无法触摸他们的文件系统,或在他们的浏览器中运行任何类型的插件。