第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

合并大量 UInt8Arrays

合并大量 UInt8Arrays

搖曳的薔薇 2022-07-08 16:00:53
我這里有一些代碼,例如,在瀏覽器中使用js-ipfs. 它目前正在工作。async function start(event) {  console.log("Starting IPFS...");  node = await Ipfs.create();  for await (const file of node.get('QmQxBX5ZKRY8k6W2UqYTMxhdFTvkmNw8X7GJN3t5UiyBpe')) {    console.log("Starting");    var content = [];    for await (const chunk of file.content) {      console.log("Gathering");      content = mergeTypedArrays(content, chunk); // slow    }    console.log("Assembling");    saveFile("arch.iso", "application/octet-stream", content);    console.log("Done");  };}// https://stackoverflow.com/a/35633935/2700296function mergeTypedArrays(a, b) {  // Checks for truthy values on both arrays  if(!a && !b) throw 'Please specify valid arguments for parameters a and b.';    // Checks for truthy values or empty arrays on each argument  // to avoid the unnecessary construction of a new array and  // the type comparison  if(!b || b.length === 0) return a;  if(!a || a.length === 0) return b;  // Make sure that both typed arrays are of the same type  if(Object.prototype.toString.call(a) !== Object.prototype.toString.call(b))      throw 'The types of the two arguments passed for parameters a and b do not match.';  var c = new a.constructor(a.length + b.length);  c.set(a);  c.set(b, a.length);  return c;}// https://stackoverflow.com/a/36899900/2700296function saveFile (name, type, data) {  if (data !== null && navigator.msSaveBlob) {    return navigator.msSaveBlob(new Blob([data], { type: type }), name);  }  var a = document.createElement('a');  a.style.display = "none";  var url = window.URL.createObjectURL(new Blob([data], {type: type}));  a.setAttribute("href", url);  a.setAttribute("download", name);  document.body.appendChild(a);  a.click();  window.URL.revokeObjectURL(url);  a.remove();}問題是,當前組裝 UInt8Array 以發(fā)送到的方法saveFile涉及在所有下載的約 2600 個塊上重新創(chuàng)建一個新的 UInt8Array,這確實很慢且效率低下。我已經(jīng)嘗試將所有這些塊推入一個數(shù)組并隨后將其組合,但我無法弄清楚如何獲取一個約 2600 個 UInt8Array 的數(shù)組并將它們展平為一個 UInt8Array。有人對我有什么建議嗎?
查看完整描述

1 回答

?
四季花海

TA貢獻1811條經(jīng)驗 獲得超5個贊

是想多了。剛剛結(jié)束直接從UInt8Arrays 的數(shù)組創(chuàng)建一個 blob。


async function start(event) {


  console.log("Starting IPFS...");

  node = await Ipfs.create();


  for await (const file of node.get('QmQxBX5ZKRY8k6W2UqYTMxhdFTvkmNw8X7GJN3t5UiyBpe')) {

    console.log("Starting");

    var content = [];

    for await (const chunk of file.content) {

      console.log("Gathering");

      content.push(chunk);

    }

    console.log("Assembling");

    saveFile(content, "archlinux-2020.04.01-x86_64.iso");

    console.log("Done");

  };


}


// https://stackoverflow.com/a/36899900/2700296

function saveFile(data, fileName) {

  if (data !== null && navigator.msSaveBlob) {

    return navigator.msSaveBlob(new Blob(data, { "type": "application/octet-stream" }), fileName);

  }

  var a = document.createElement('a');

  a.style.display = "none";

  var url = window.URL.createObjectURL(new Blob(data, {type: "application/octet-stream"}));

  a.setAttribute("href", url);

  a.setAttribute("download", fileName);

  document.body.appendChild(a);

  a.click();

  window.URL.revokeObjectURL(url);

  a.remove();

}


查看完整回答
反對 回復 2022-07-08
  • 1 回答
  • 0 關(guān)注
  • 433 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號