通过ArrayBuffer构造的Blob对象,为什么size变大了?

后端有一个exe文件需要我下载,我通过fetch获取了其ReadableStream,并通过reader获取了原始的ArrayBuffer,长度是7080748,但当我构造blob准备进行下载时,文件的size大小却增加到了252162751,我无论如何都无法复原文件,文件的MIME类型是application/x-msdownload;下面是我的代码:

const proccessor = async (reader) => {
  let res = [];
  await reader.read().then(async function processRead({ done, value }) {
    if (done) {
       return res;
    }
    res = [...res, ...value];
      await reader.read().then(processRead);
   });
   return res;
 };

fetch(testUrl, {method: 'GET'})
  .then(async (res) => {
        if (res.status === 200) {
          const reader = res.body.getReader();
          const result = await proccessor(reader);
          return result;
        } else {
          throw new Error('解析错误');
        }
      })
      .then((stream) => {
        console.log('原始文件流', stream); // 此时stream的长度为7080748
        const uint8Stream = new Uint8Array(stream);
        const file = new File([uint8Stream], 'test.exe', { type: 'application/x-msdownload' });
        console.log(file.size) // 此时长度变为了252162751
      });
阅读 3.1k
1 个回答
fetch('https://segmentfault.com/q/1010000043445854?https://www.lilnong.top')
    .then(v=>v.blob())
    .then(v=>console.log(v, URL.createObjectURL(v)))

没看懂你反复处理了半天什么

image.png

推荐问题
宣传栏