如何使用多个.chunk文件创建文件?

时间:2019-08-12 14:49:08

标签: javascript node.js node-fetch

我有更多的.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.

0 个答案:

没有答案