HTML5 FIle API:读取文件时出现安全性错误

时间:2011-06-21 15:27:25

标签: javascript html5 filereader fileapi

问题解决了,请阅读评论

我对HTML5文件API的第三个问题: 我仍在Mac OS X Snow Leopard上使用Chrome 12,我仍在尝试使用HTML5文件API读取文件,但因为出现“SECURITY_ERR”而调用FileHandler.error()。 我尝试读取的文件是我桌面上的常规.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

1 个答案:

答案 0 :(得分:12)

如果您正在测试来自file://的应用,则可以使用以下标记运行Chrome:--allow-file-access-from-files --allow-file-access。这只应用于测试目的。