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


问题解决了,看了评论

我对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


1735
2018-06-21 15:27


起源

问题解决了!我刚刚将脚本上传到一个完美的网站空间。 - Claudio Albertin
如果您正在测试应用程序 file://,您可以使用以下标志运行Chrome: --allow-file-access-from-files --allow-file-access。这应仅用于测试目的。 - ebidel
@ebidel您应该添加该评论作为答案,以便这篇文章可以关闭:) - Matthew Caruana Galizia


答案:


如果您正在测试应用程序 file://,您可以使用以下标志运行Chrome: --allow-file-access-from-files  --allow-file-access。这应仅用于测试目的。


12
2017-10-07 19:07



所以基本上没有办法在网站上使用文件阅读器?您不能要求您的用户使用特殊标志启动其Chrome浏览器:S - sebpiq
没有 file:// 协议将仅在本地测试网站时使用,无需任何服务器运行,一旦部署,文件将被转移 http:// 协议 - Valentin Jacquemin
如果平板电脑执行HTML5应用程序,它将使用file://或http://协议吗?如果是后者,它也会遇到这个错误吗? - Extrakun
+1文件/ http提示。但我创建了一个Chrome快捷方式,并添加了提到的标志作为参数,但它不起作用。我该如何使用这些旗帜? - kolenda