Chrome 文件阅读器

编程入门 行业动态 更新时间:2024-10-26 12:21:18
本文介绍了Chrome 文件阅读器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

谁能给我一个使用 FileReader API 来获取 chrome 文件内容的例子?

Can someone give me an example of using the FileReader API go get contents of a file in chrome?

它似乎为我返回了 undefined.

<!doctype html> <html> <script> function handle_files(files) { console.log(files) reader = new FileReader() ret = [] for (i = 0; i < files.length; i++) { file = files[i] console.log(file) text = reader.readAsText(file) //readAsdataURL console.log(text) //undefined ret.push(text) } console.log(ret) // [undefined] } </script> <body> FileReader Test <input type="file" onchange="handle_files(this.files)"> </body> </html>

推荐答案

我的问题是我认为 FileReader 是同步的.这是正确的方法.如果您使用的是 chrome,则此代码必须在服务器(本地主机或站点)上运行.它不适用于本地文件.

My problem was that I assumed FileReader was sychronous. Here is the right way to do it. If you are on chrome, this code has to be running on a server (localhost or on a site). It won't work with a local file.

<!doctype html> <html> <script> function handle_files(files) { for (i = 0; i < files.length; i++) { file = files[i] console.log(file) var reader = new FileReader() ret = [] reader.onload = function(e) { console.log(e.target.result) } reader.onerror = function(stuff) { console.log("error", stuff) console.log (stuff.getMessage()) } reader.readAsText(file) //readAsdataURL } } </script> <body> FileReader that works! <input type="file" multiple onchange="handle_files(this.files)"> </body> </html>

更多推荐

Chrome 文件阅读器

本文发布于:2023-07-15 23:11:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1115330.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:阅读器   文件   Chrome

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!