2011-06-21 45 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