同时上传多张图片损坏

时间:2019-02-05 05:45:35

标签: node.js express

尝试使用busboy上传多个文件,该文件可能很大。因此,我正在异步上传,我正在使用file.on('data')

当我以一定的延迟(> 3s)连续请求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);
});

0 个答案:

没有答案