multer req.body始终显示为空 [英] multer req.body showing empty always
本文介绍了multer req.body始终显示为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在学习nodejs并表示,我正尝试执行文件上传,以下是我的代码
I am learning nodejs and express, i was trying to do file upload below is my code
const express = require('express');
const multer = require('multer');
const app = express();
const port = process.env.PORT||3000;
const path = require('path');
const fs= require('fs');
const xlsx = require('node-xlsx');
const bodyParser=require('body-parser')
const storage=multer.diskStorage({
destination:'./public/uploads',
filename:function(req,file,cb){
cb(null,file.fieldname+'-'+Date.now()+path.extname(file.originalname))
}
})
const upload=multer({
storage:storage
})
app.set('port',port);
app.set('view engine', 'ejs');
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static('./public'));
app.post('/uploadTest', upload.single('myImage'),function (req, res, next) {
setTimeout(function(){
console.log(req.files);
console.log(req.body);// {"someParam": "someValue"}
res.send(req.body);
}, 3000);
// req.file is the `avatar` file
// req.body will hold the text fields, if there were any
})
var server=app.listen(app.get('port'),(req,res)=>{
console.log(`server started at port ${app.get('port')}`)
});
下面是我的HTML
<form action="/uploadTest" method="post" enctype="multipart/form-data">
<input type="file" name="myImage"/>
<input type="submit" value="Add File"/>
</form>
所以我的问题是console.log(req.body);// {"someParam": "someValue"}
,这总是在Google上显示很多空对象,但无法使其工作
so my issue is console.log(req.body);// {"someParam": "someValue"}
this is always showing empty object i google alot but didnt able to make it working
这是我得到的输出
[ { fieldname: 'myImage',
originalname: 'SampleXLSFile_19kb.xls',
encoding: '7bit',
mimetype: 'application/vnd.ms-excel',
destination: './public/uploads',
filename: 'myImage-1534000938255.xls',
path: 'public\\uploads\\myImage-1534000938255.xls',
size: 19456 } ]
{}
谢谢您的帮助
推荐答案
在您的app.js中更改此内容:
In your app.js change this :
const upload=multer({
dest:'./public/uploads'
})
然后尝试
这篇关于multer req.body始终显示为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文