簡體   English   中英

如何在 javascript 中傳輸大文件下載的文件塊並重建它?

[英]How to transfer file chunks for large file download and reconstruct it in javascript?

I need to download large files using files chunks for which JAVA spring server is developed and files are recived at UI via REST api. 尋找解決方案來加入 javascript 中的所有塊

我不確定您的要求,但希望這會對您有所幫助

首先,您需要使用async function 中的 fetch 調用來獲取文件

response = await fetch(url);

然后之后您將能夠從響應中獲取閱讀器const reader = response.body.getReader();

您可以通過測量該存儲總文件大小的進度來跟蹤獲取

const contentLength = +response.headers.get('Content-Length');

為了跟蹤接收到的字節let receivedLength = 0;

存儲收到的塊

let chunks = []; // array of received binary chunks (comprises the body)

之后做一個while循環,它將無休止地運行

while(true) {
          const {done, value} = await reader.read();

          if (done) {
            break;
          }

          chunks.push(value);
          receivedLength += value.length;
            
          let compleated = Math.round(receivedLength/contentLength*100);

          console.log(`Received ${receivedLength} of ${contentLength}`)
        }

 after the while loop has broken you can check if the transfer is fully completed or not

然后你可以從我們存儲數據塊的數組中創建一個 blob

let blob = new Blob(chunks);

那么你可以使用這個blob

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM