Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

nodejs stream合并分片遇到的问题

当客户端对压缩包进行分片传到服务端时,使用stream流合并文件分片,没有任何报错,但最后打开压缩包发现缺失了部分资源,压缩包的大小却和上传前一样...有大佬踩过这个坑吗!!

问题展示

image.png
image.png

相关代码(网上)
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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

试下用现成的文件拼接工具检验一下客户端上传的分片内容和顺序是否正确。如 linux 的 cat 命令:

cat chunk1 chunk2 > output.rar

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...