尝试使用busboy实现文件上传,单个文件上传运行良好。在实现“多文件”上传方面需要帮助。
检查了诸如multer之类的各种选项,但没有明确说明如何处理多个文件上传的地方。
下面的代码需要进行哪些代码修改以确保
1)多个文件上传选项已添加到表单代码中
2)busboy.on('finish'function(){})代码块可确保所有 文件已正确上传到目标文件夹
以下是单个文件上传的示例代码
var path = require('path');
var fs = require('fs');
var os = require('os');
var express = require('express');
var app = express();
var Busboy = require('busboy');
app.get('/', function(req,res){
res.send('<html<head></head><body>\
<form method="POST" enctype="multipart/form-data">\
<input type="Text" name="textfield"><br />\
<input type="file" name="filefield1"><br />\
<input type="submit">\
</form>\
</body></html>');
res.end();
});
app.post('/',function(req,res){
var busboy = new Busboy({headers:req.headers});
busboy.on('file',function(fieldname,file,filename,encoding,mimetype){
var saveTo = path.join('./tmp',filename);
console.log('Uploading:' + saveTo);
file.pipe(fs.createWriteStream(saveTo));
});
busboy.on('finish',function(){
console.log('Upload complete');
res.writeHead(200,{'Connection': 'close' });
res.end('Whole file upload process is finished now!');
});
return req.pipe(busboy);
});
var server = app.listen(3000,function() {
var host = server.address().address
var port = server.address().port
console.log('Example app listening at http://%s:%s', host, port)
});