2011-06-20 13 views
0

Mac OS XでChrome 12を使用しましたが、ドキュメント内にjQuery 1.6.1が含まれています。HTML5 File API:エラーが発生した後にFileReaderオブジェクトが定義されていません

次のコードを使用してファイルを読み取ろうとしましたが、ファイルの読み取り中にエラーが発生してthis.error.onerror()が呼び出されましたが、FileReader-Object this.readerはもう存在しません。私はエラーを得ることができません。私はまた、なぜエラーが発生するかわからない、それは私が読んでほしい通常のテキスト文書です。それは新しい機能が(新しいスコープで)ですので、thisは外と同じではありません、.onerrorインサイド http://cl.ly/0j1m3j0o3s071Y1K3A25

+0

、やりますあなたは同じ問題がありますか? Chromeの代わりにFFを使用する場合は、同じ問題がありますか?そうでない場合は、Chromeの同じ起源のポリシーに踏み込んでいる可能性があります。ここでの答えとコメントを参照してください:http://stackoverflow.com/questions/4100927/chrome-filereader/4101266#comment10572715_4101266 –

答えて

0

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 a file.'); 
     console.log(this.reader.error); 
    } 

    this.reader.onload = this.upload; 
    this.reader.onerror = this.error; 
} 

このコードは、次のコンソール出力を作成します。

このように静的に設定することにより、thisを追跡:

var _this = this; // _this won't change 
this.reader.onerror = function() { 
    console.log('An error occurred while reading a file.'); 
    console.log(_this.reader.error); 
} 
+0

私はそれが動作するはずだと思うようにコードを更新しました(少なくともthis.uploadで動作します) )。しかし、エラーはまだ発生し、コンソールの出力は同じです。 –

+0

あなたはそれが 'undefined'ですか? '_this'は' .error'の外で定義する必要があることに注意してください。 – pimvdb

+0

「複数のオブジェクト」はどういう意味ですか? – pimvdb

0

これはそれを行うための正しい方法でなければなりません:あなたは、Webサーバー上のファイルをホストしている場合

reader.onerror = function(event) { 
    console.error("File could not be read: " + event.target.error); 
}; 
関連する問題