2011-06-21 49 views
11

问题解决了,阅读评论HTML5文件API:安全性错误时读取文件

第三个问题我与HTML5文件API: 我仍然使用Chrome 12在Mac OS X雪豹和我m仍然尝试使用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

+0

问题解决了!我只是将脚本上传到了一个完美的web空间。 –

+3

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

+0

@ebidel您应该添加该评论作为答案,所以这篇文章可以关闭:) –

回答

12

如果你从file://测试的应用程序,你可以用下面的标志运行Chrome浏览器:--allow-file-access-from-files--allow-file-access。这应该只用于测试目的。

+0

所以基本上没有办法在网站上使用文件阅读器?您不能要求您的用户使用特殊标志启动Chrome浏览器:S – sebpiq

+0

没有任何服务器运行时,只有在本地测试网站时才会使用'file://协议,部署后文件将通过' http://协议 –

+0

如果平板电脑执行HTML5应用程序,它会使用file://或http://协议吗?如果是后者,它是否也会遇到这个错误? – Extrakun