首页 > 解决方案 > 如何使用多个 .chunk 文件创建文件?

问题描述

我有更多 .chunk 文件,我使用 node-fetch 获取并使用这些文件,我想通过某种方式“将它们收集在一起”来创建另一个文件。

我试过这个:

var fileStream = await fs.createWriteStream(`./paks/${file.Filename.slice(26)}`)
//below code is in a loop, above isn't in a loop
await fetch(chunkURL)
    .then(async res => {
        if(chunks.indexOf(chunk) == chunks.length - 1) await res.body.pipe(fileStream, { end: true })
        else await res.body.pipe(fileStream, { end: false })
    })

但是文件在完成后已损坏,我也尝试使用 flieStream.write(res.body, { end: true }) 而不是 res.body.pipe(fileStream, { end: true }) 但我收到以下错误:

(node:12110) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be one of type string or Buffer. Received type object
    at validChunk (_stream_writable.js:265:10)
    at WriteStream.Writable.write (_stream_writable.js:300:21)
    at /root/api/getUpdate.js:97:87
    at processTicksAndRejections (internal/process/task_queues.js:85:5)
    at async /root/api/getUpdate.js:94:57
(node:12110) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:12110) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

标签: javascriptnode.jsnode-fetch

解决方案


推荐阅读