2016-08-07 8 views
0

私はFileReaderを使用して、いくつかのファイルを順番に読むことを約束しています。問題は、読取り操作を複数のチャンクで分割して、それらを実行可能にする必要があることです。そうすることで、私はPromiseチェーンを失います。これは、コンソールログhere、次にcatched(私がチェーンを失ったことを意味する)を取得し、次にinside、次にfinishedというコードで発生します。どういうわけかアップロードの約束は尊重されません。ここでJavascript、スプライスされたFileReaderを使用して、大きなファイルを約束してください。

は、それがコードである

var reader = new FileReader(); 
reader.onloadend = function(e) { 
    if (e.target.readyState == 2) { 
    console.log('inside') 
    start = temp_end; 
    temp_end = start + BYTES_PER_CHUNK; 
    if (temp_end > end) temp_end = end; 
    upload(); 
    } 
}; 

Array.reduce(function(promise, item) { 
    start = 0; 
    temp_end = start + BYTES_PER_CHUNK; 
    end = parseInt(totalsize); 
    if (temp_end > end) temp_end = end; 
    uploading_file = item; 
    return upload() 
    .then(function(){ 
    console.log('not shown'); 
    }) 
    .catch(console.log('catched')); 
},0); 

function upload() { 
    return new Promise(function(resolve,reject) { 
    if (start < end) { 
     console.log('here') 
     var chunk = uploading_file.slice(start, temp_end); 
     reader.readAsBinaryString(chunk); 
    } else { 
     console.log('finished') 
     uploading_file = null; 
     resolve(); 
    } 
    } 
} 

EDIT1(最後のEDITに行ってください、私はそれにもかかわらず、元のテキストを保つ):私はしようとしている新しいコード、まだにconsole.log stop 1uploadの前に表示されますそしてhere 2

 var start = 0; 
     var temp_end = start + BYTES_PER_CHUNK; 
     var end = parseInt(item.size); 
     if (temp_end > end) temp_end = end; 
     uploading_file = item; 
     console.log('here 1') 
     Promise.resolve().then(function() { 
      return upload(); 
     }) 
     .then(function(){ 
      console.log('here 2') 
     }) 
     .catch(console.log('stop 1')); 

     function upload() { 
      console.log('upload') 
      if (start < end) { 
       var chunk = uploading_file.slice(start, temp_end); 
       var reader = new FileReader(); 
       reader.readAsArrayBuffer(chunk); 
       reader.onload = function(e) { 
        if (e.target.readyState == 2) { 
         start = temp_end; 
         temp_end = start + BYTES_PER_CHUNK; 
         if (temp_end > end) temp_end = end; 
         return upload(); 
        } 
       } 
      } else { 
       console.log('finished') 
       uploading_file = null; 
       return Promise.resolve(); 
      } 
     } 

EDIT2:キャッチログが原因の機能の欠如()にありました。次のコードはうまくいくようですが、私は最後に興味がある値contentを見ることができません。

 var item; // item is a file 
     var BYTES_PER_CHUNK = 100000; 
     var start = 0; 
     var temp_end = start + BYTES_PER_CHUNK; 
     var end = parseInt(item.size); 
     if (temp_end > end) temp_end = end; 
     var content = ''; // to be filled by the content of the file 
     var uploading_file = item; 
console.log('here 1'); 
     Promise.resolve().then(function() { 
console.log('here 2'); 
      return upload(); 
     }) 
     .then(function(content){ 
console.log('here 4'); 
      console.log(content); // this is empty (!!) 
     }) 
     .catch(function(){ 
      console.log('stop 1') 
     }); 

     function upload() { 
      if (start < end) { 
       var chunk = uploading_file.slice(start, temp_end); 
       var reader = new FileReader(); 
       reader.readAsArrayBuffer(chunk); 
       reader.onload = function(e) { 
        if (e.target.readyState == 2) { 
         content += new TextDecoder("utf-8").decode(e.target.result); 
         start = temp_end; 
         temp_end = start + BYTES_PER_CHUNK; 
         if (temp_end > end) temp_end = end; 
         return upload(); 
        } 
       } 
      } else { 
       uploading_file = null; 
       console.log(content); // it shows the content of the file 
console.log('here 3'); 
       return Promise.resolve(content); 
      } 
     } 
+2

私はあなたがしようとしていることを理解することは本当に難しいと思っています。宣言されていない変数、共有状態、不正なコードがたくさんあります( 'Array.reduce'?' Array'は実際にあなたの実際のコードの変数ですか?)。そして、「アップロード」はアップロードとは関係がないように見え、返された約束は解決しない場合もあります。私は試みを推測するには刺すだろうが、あなたがコードの目的が何であるかを明確にすることができれば。 – Jacob

+0

最後のコードは最後のEDITに移動してください。いずれにしても、Array.reduceは配列があり、reduceを使用したことを意味していましたが、コードのこの部分は実際に分離したい問題には重要ではありません。最後のコードで、私が達成したいことがより明確になることを願っていますか?最終的な目標は、チャンクでファイルを読み取って、約束によってこれを制御し続けることです。 – Gerard

+0

今よりもはっきりと感謝します。 – Jacob

答えて

1

あなたupload()機能は常に約束を返しません:

問題は、約束は確かにここで働いていないということです、にconsole.logはコードが

here 1 
here 2 
here 4 
here 3 

言います。 elseの条件で行いますが、if条件はありません。あなたは返信陳述書を持っていますが、コールバックの内部にあるので、それはuploadの呼び出し元によって受信されません。

これにそれを変更してみてください:

function upload() { 
    if (start < end) { 
    return new Promise(function (resolve, reject) { 
     var chunk = uploading_file.slice(start, temp_end); 
     var reader = new FileReader(); 
     reader.readAsArrayBuffer(chunk); 
     reader.onload = function(e) { 
     if (e.target.readyState == 2) { 
      content += new TextDecoder("utf-8").decode(e.target.result); 
      start = temp_end; 
      temp_end = start + BYTES_PER_CHUNK; 
      if (temp_end > end) temp_end = end; 
      resolve(upload()); 
     } 
     } 
    }); 
    } else { 
    uploading_file = null; 
    console.log(content); // it shows the content of the file 
    return Promise.resolve(content); 
    } 
} 

さて、upload代わりに、常にundefinedの約束を返します。

+0

すばらしい。これだよ。大感謝!私はここでArray.reduceを使って悩んでいます。これはきれいに解決されて以来、私はそれについて新しい質問を開いています。http://stackoverflow.com/questions/38822187/javascript-uploading-several-files-within-an-array-reduce-with-promises-howあなたに時間がある場合に備えて – Gerard

関連する問題