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

Cla*_*tin 12 javascript html5 filereader fileapi

问题解决了,看了评论

我在HTML5文件API中遇到的第三个问题:我仍在Mac OS X Snow Leopard上使用Chrome 12,我仍在尝试使用HTML5文件API读取文件,但因为"SECURITY_ERR"而调用了FileHandler.error() occurres.我尝试读取的文件是来自桌面的常规.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);
}
Run Code Online (Sandbox Code Playgroud)

该代码生成以下控制台输出:http: //cl.ly/1x1o2F0l2m3L1T2c0H0i

ebi*_*del 13

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