【问题标题】:HTML5 FIle API: Security Error while reading a fileHTML5 文件 API:读取文件时出现安全错误
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 所以基本上没有办法在网站上使用文件阅读器?您不能要求您的用户使用特殊标志启动他们的 chrome 浏览器:S
  • 没有file://协议只会在本地测试网站时使用,没有任何服务器运行,一旦部署文件将通过http://协议传输
  • 如果平板电脑执行 HTML5 应用程序,它会使用 file:// 还是 http:// 协议?而如果是后者,会不会也遇到这个错误呢?
  • +1 表示文件/http 提示。但是我创建了一个 Chrome 快捷方式并添加了提到的标志作为参数,但它不起作用。我应该如何使用这些标志?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-14
  • 2015-06-08
相关资源
最近更新 更多