multer 不工作
multer is not working
大家好,我需要你的帮助,我无法解决我的 multer 问题,文件未上传,req.file 始终未定义。
这是我的代码
addProps.js
const multer = require('multer')
const upload = multer({ dest: 'public/imahe/' })
router.post('/add', isAuthenticated, upload.single('imahe'), (req, res, next) => {
console.log(req.body)
console.log(req.files)
const member = req.user
Members.findOne({})
const props = new Property({
pname: req.body.pname,
ptype: req.body.ptype,
price: req.body.price,
pterm: req.body.pterm,
location: req.body.location,
description: req.body.description,
gallery: req.body.imahe,
owner: member._id
})
props.save( (err, props) => {
if (err) {
console.log('Unable to register your data: ' + err)
throw err
}
console.log('Property Added Successful!')
res.redirect('/dashboard')
})
})
addproperty.ejs
<% include header%>
<div class="row">
<form class="col s12" method="POST" action="/property/add">
<div class="row">
<div class="file-field input-field">
<div class="btn">
<span>File</span>
<input type="file" name="imahe">
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text">
</div>
</div>
</div>
<button class="btn waves-effect waves-light" type="submit" name="action" value="submit">Submit
<!-- <i class="material-icons right">send</i> -->
</button>
</form>
</div>
<% include foot%>
req.file 或 req.files 总是在我这边返回未定义,我没有任何其他错误,我试图找到一些解决方案
顺便说一句,这是我的 header
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization')
if (req.method === 'OPTIONS') {
res.header('Access-Controll-Allow-Methods', 'PUT, POST, PATCH, DELETE, GET')
return res.status(200).json({})
}
next()
})
您需要将 enctype 设置为 multipart/form-data 格式。
When you are writing client-side code, all you need to know is use multipart/form-data when your form includes any elements.
希望以下代码片段对您有所帮助。
<form class="col s12" enctype='multipart/form-data' method="POST" action="/property/add" >
详情请参考this回答
大家好,我需要你的帮助,我无法解决我的 multer 问题,文件未上传,req.file 始终未定义。 这是我的代码
addProps.js
const multer = require('multer')
const upload = multer({ dest: 'public/imahe/' })
router.post('/add', isAuthenticated, upload.single('imahe'), (req, res, next) => {
console.log(req.body)
console.log(req.files)
const member = req.user
Members.findOne({})
const props = new Property({
pname: req.body.pname,
ptype: req.body.ptype,
price: req.body.price,
pterm: req.body.pterm,
location: req.body.location,
description: req.body.description,
gallery: req.body.imahe,
owner: member._id
})
props.save( (err, props) => {
if (err) {
console.log('Unable to register your data: ' + err)
throw err
}
console.log('Property Added Successful!')
res.redirect('/dashboard')
})
})
addproperty.ejs
<% include header%>
<div class="row">
<form class="col s12" method="POST" action="/property/add">
<div class="row">
<div class="file-field input-field">
<div class="btn">
<span>File</span>
<input type="file" name="imahe">
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text">
</div>
</div>
</div>
<button class="btn waves-effect waves-light" type="submit" name="action" value="submit">Submit
<!-- <i class="material-icons right">send</i> -->
</button>
</form>
</div>
<% include foot%>
req.file 或 req.files 总是在我这边返回未定义,我没有任何其他错误,我试图找到一些解决方案
顺便说一句,这是我的 header
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization')
if (req.method === 'OPTIONS') {
res.header('Access-Controll-Allow-Methods', 'PUT, POST, PATCH, DELETE, GET')
return res.status(200).json({})
}
next()
})
您需要将 enctype 设置为 multipart/form-data 格式。
When you are writing client-side code, all you need to know is use multipart/form-data when your form includes any elements.
希望以下代码片段对您有所帮助。
<form class="col s12" enctype='multipart/form-data' method="POST" action="/property/add" >
详情请参考this回答