当客户端对压缩包进行分片传到服务端时,使用stream流合并文件分片,没有任何报错,但最后打开压缩包发现缺失了部分资源,压缩包的大小却和上传前一样...有大佬踩过这个坑吗!!
问题展示
相关代码(网上)
let chunkPaths = chunks.map(function (name) {
return path.join(process.env.IMAGESDIR, name)
});
// 采用Stream方式合并
let targetStream = fs.createWriteStream(path.join(process.env.IMAGESDIR, fileName));
const readStream = function (chunkArray, cb) {
let path = chunkArray.shift();
let originStream = fs.createReadStream(path);
originStream.pipe(targetStream, {end: false});
originStream.on("end", function () {
// 删除文件
fs.unlinkSync(path);
if (chunkArray.length > 0) {
readStream(chunkArray, callback)
} else {
cb && cb()
}
});
};
readStream(chunkPaths, callback);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…