【发布时间】:2011-09-19 15:35:45
【问题描述】:
问题解决了,看评论
HTML5 File API 的第三个问题: 我仍在 Mac OS X Snow Leopard 上使用 Chrome 12,并且仍在尝试使用 HTML5 File API 读取文件,但 FileHandler.error() 被调用,因为发生了“SECURITY_ERR”。 我尝试读取的文件是我桌面上的常规 .txt 文件,但它不能与其他文件一起使用,尽管我可以使用常规应用程序打开它们。
function FileHandler(files, action) {
console.log('FileHandler called.');
this.files = files;
this.reader = new FileReader();
this.action = action;
this.handle = function() {
console.log('FileHandler.handle called.');
for (var i = 0; i < this.files.length; i++) {
this.reader.readAsDataURL(files[i]);
}
}
this.upload = function() {
console.log('FileHandler.upload called.');
console.log(this.reader);
data = {
content: this.reader.result
}
console.log(data);
}
this.error = function() {
console.log('An error occurred while reading the file.');
console.log(this.reader.error);
}
this.reader.onload = this.upload.bind(this);
this.reader.onerror = this.error.bind(this);
}
代码生成以下控制台输出: http://cl.ly/1x1o2F0l2m3L1T2c0H0i
【问题讨论】:
-
问题解决了!我刚刚将脚本上传到一个完美运行的网络空间。
-
如果您正在测试来自
file://的应用程序,您可以使用以下标志运行 Chrome:--allow-file-access-from-files --allow-file-access。这只能用于测试目的。 -
@ebdel 您应该添加该评论作为答案,以便可以关闭此帖子:)
标签: javascript html filereader fileapi