使用Express 4.0上传的文件:req.file未定义我试图获得一个使用Express 4.0的简单文件上传机制,但是我一直undefined为req.files在app.post尸体。以下是相关代码:var bodyParser = require('body-parser');var methodOverride = require('method-override');
//...app.use(bodyParser({ uploadDir: path.join(__dirname, 'files'), keepExtensions: true })); app.use(methodOverride());
//...app.post('/fileupload', function (req, res) {
console.log(req.files);
res.send('ok'); });.。以及附带的帕格代码:form(name="uploader", action="/fileupload", method="post", enctype="multipart/form-data")
input(type="file", name="file", id="file")
input(type="submit", value="Upload")解感谢mscdex下面,我转向使用busboy而不是bodyParser:var fs = require('fs');var busboy = require('connect-busboy');//...app.use(busboy()); //...app.post('/fileupload', function(req, res) {
var fstream;
req.pipe(req.busboy);
req.busboy.on('file', function (fieldname, file, filename) {
console.log("Uploading: " + filename);
fstream = fs.createWriteStream(__dirname + '/files/' + filename);
file.pipe(fstream);
fstream.on('close', function () {
res.redirect('back');
});
});});
3 回答
温温酱
TA贡献1752条经验 获得超4个赞
body-parser
做
express-busboy
console.log(req.files)
{ file: { fieldName: 'file', originalFilename: '360px-Cute_Monkey_cropped.jpg', name: '360px-Cute_Monkey_cropped.jpg' path: 'uploads/6323-16v7rc.jpg', type: 'image/jpeg', headers: { 'content-disposition': 'form-data; name="file"; filename="360px-Cute_Monkey_cropped.jpg"', 'content-type': 'image/jpeg' }, ws: WriteStream { /* ... */ }, size: 48614 } }
console.log(req.files)
{ file: { field: 'file', filename: '360px-Cute_Monkey_cropped.jpg', file: 'uploads/9749a8b6-f9cc-40a9-86f1-337a46e16e44/file/360px-Cute_Monkey_cropped.jpg', mimetype: 'image/jpeg', encoding: '7bit', truncated: false uuid: '9749a8b6-f9cc-40a9-86f1-337a46e16e44' } }
- 3 回答
- 0 关注
- 2088 浏览
添加回答
举报
0/150
提交
取消