javascript 使用什么来代替 Safari 的 FileReader?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7734150/
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-10-26 01:07:20  来源:igfitidea点击:

What to use instead of FileReader for Safari?

javascriptsafarifilereader

提问by Emma

(Am new to web programming, so apologies for any lack of rudimentary knowledge.)

(我是 Web 编程的新手,因此对缺乏基本知识表示歉意。)

My page allows a user to select a file that is then read clientside & displayed in a textbox on the page. The easiest way I found to do this was to use a FileReader object, which works fine in Firefox and Chrome.

我的页面允许用户选择一个文件,然后在客户端读取并显示在页面上的文本框中。我找到的最简单的方法是使用 FileReader 对象,它在 Firefox 和 Chrome 中运行良好。

This doesn't work in Safari (yet), so what should I do instead?

这在 Safari 中(还)不起作用,那么我应该怎么做呢?

//When the eventlistener detects a change in the input file...
var file = evt.target.files[0]
var reader = new FileReader();
reader.onload = function (e){document.getElementById('data').value = e.target.result};
reader.readAsText(file);

Relevant Notes:

相关说明:

  • I'm working with Safari for windows
  • Right now the page is local as is the file to read. Chrome had issues with this until I used the flag --allow-file-access-from-files
  • 我正在使用 Safari for windows
  • 现在页面是本地的,就像要读取的文件一样。Chrome 遇到了这个问题,直到我使用了标志 --allow-file-access-from-files

采纳答案by Quickredfox

Sadly, the only answer I can come up with will hog some extra bandwidth.

可悲的是,我能想出的唯一答案会占用一些额外的带宽。

Firstly, use something like if (typeof FileReader !== "undefined"or Modernizrto follow your normal flow for the browsers that DO support FileReader. Otherwise, POST the file via AJAX to some server-side script that echoes back the contents.

首先,使用类似if (typeof FileReader !== "undefined"Modernizr 的东西来遵循支持 FileReader 的浏览器的正常流程。否则,通过 AJAX 将文件 POST 到一些服务器端脚本,以回显内容。

So for compliant browsers, you get to save yourself some bandwidth and for non-compliant browsers you have to take one for the team.

因此,对于兼容的浏览器,您可以为自己节省一些带宽,而对于不兼容的浏览器,您必须为团队使用一个。

回答by Tony

Another solution is to use "A FileReader polyfill for Internet Explore and Safari using Flash"

另一种解决方案是使用“A FileReader polyfill for Internet Explore and Safari using Flash”

See: https://github.com/Jahdrien/FileReader

见:https: //github.com/Jahdrien/FileReader