node.js - 同时上传多个图像被损坏
问题描述
尝试使用busboy上传多个文件,文件可能很大所以,我正在异步上传,我正在使用file.on('data')
当我以一些延迟(> 3 秒)连续请求 API 时,该功能工作正常,如果我连续(无延迟)/并行/同时请求 API,则某些图像丢失或损坏。
app.post('/upload', function (req, res) {
var busboy = new Busboy({ headers: req.headers });
busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
var writeStream = [];
writeStream[fieldname] = fs.createWriteStream('./tmp/tmp' + Date.now());
console.log('File [' + fieldname + ']: filename: ' + filename + ', encoding: ' + encoding + ', mimetype: ' + mimetype);
file.on('data', function (data) {
console.log('File [' + fieldname + '] got ' + data.length + ' bytes');
writeStream[fieldname].write(data);
});
file.on('end', function () {
console.log('File [' + fieldname + '] Finished');
writeStream[fieldname].end();
});
});
busboy.on('finish', function () {
console.log('Upload complete');
res.writeHead(200, { 'Connection': 'close' });
res.end("That's all folks!");
});
return req.pipe(busboy);
});
解决方案
推荐阅读
- php - PHP 修复警告:ini_set():会话处于活动状态
- r - 使用向量化函数过滤每个列表中的不同值
- swagger - swagger/openapi 3.0 如何从 YML 中配置的数组中选择服务器
- typescript - Angular6:handleError 无法读取注入的服务?
- firebase - 找不到 com.google.firebase:firebase-messaging:17.1.0
- python - 使用颜色列 Matplotlib Python 绘制矩形
- android-studio - 如何在 Flutter 中运行不同的 dart 文件?
- android - Android MotionEvent.CREATOR.createFromParcel 因“读取 MotionEvent 包裹失败”而失败。
- python - 如何解决“远程:您不允许上传代码”。GitLab CI/CD 作业出错?
- angularjs - 错误:转换已被不同的转换取代