HTML5 FIle API:读取文件时出现安全性错误 [英] HTML5 FIle API: Security Error while reading a file
问题描述
问题解决,阅读评论
我对HTML5文件API的第三个问题:
我仍然使用Chrome 12在Mac OS X Snow Leopard上,我仍在尝试使用HTML5文件API读取文件,但因为出现SECURITY_ERR而调用FileHandler.error()。
我尝试读取的文件是我桌面上的常规.txt文件,但它不能与其他文件一起使用,尽管我可以使用常规应用程序打开它们。
The third problem I have with the HTML5 File API: I still use Chrome 12 on Mac OS X Snow Leopard and I'm still trying to read files with the HTML5 File API, but FileHandler.error() get called because a "SECURITY_ERR" occurres. The file I try to read is a regular .txt file from my desktop, but it neither works with other files although I can open them with regular applications.
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
The code generates the following console output: http://cl.ly/1x1o2F0l2m3L1T2c0H0i
推荐答案
如果您正在从 file://
测试应用,则可以使用以下标志运行Chrome: - allow-file-access-from -files
- allow-file-access
。这只应用于测试目的。
If you're testing an app from file://
, you can run Chrome with the following flags: --allow-file-access-from-files
--allow-file-access
. This should only be used for testing purposes.
这篇关于HTML5 FIle API:读取文件时出现安全性错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!