MulterError:失眠中wrappedFileFilter处出现意外字段



我正试图上传一张用户在应用程序中注册的图片。当我试图在失眠中测试它时,我会得到以下错误:MulterError:wrappedFileFilter的意外字段

我一直在网上阅读,我知道当客户端提供的字段名和服务器期望的字段名不匹配时会出现这个错误。然而,我已经检查了很多次我的代码,并且我在服务器和客户端中给出的名称(失眠(是相同的:";图像";。你知道我还能试什么吗?

这是我的代码:

const storage = multer.diskStorage({
destination: './client/public/img',
filename: (req, file, cb) => {
console.log(file)
cb(null, file.originalname)
}
})
const fileFilter = (req, file, cb) => {
if(file.mimetype == "image/jpeg" || file.mimetype == "image/png") {
cb(null, true)
} else {
cb(null, false)
}
}
const upload = multer({ storage: storage, fileFilter: fileFilter, destination: './client/public/img' })
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

//sign up
routes.post("/register", upload.single("image"), (req, res) => {
let { user_name, email, password, password2 } = req.body;
let { image } = req.file.filename
let errors = []
//check required inputs
if(!user_name || !email || !password || !password2){
errors.push({message: "Please fill all required fields"})
res.send({message: "Please fill all required fields"})   
}
//check passwords
if(password != password2) {
errors.push({message: "Passwords do not match"})
res.send({message: "Passwords do not match"})
}
if(errors.length>0) {
console.log(errors);
} else {
if(email) {
db(`SELECT * FROM user WHERE email = "${email}"`)
.then((results) => {
if(results.data.length>0){
res.send("Email exists")
} else {
bcrypt.hash(password, saltRounds, (err, hash) => {
password = hash
db(`INSERT INTO user (user_name, email, password, image) VALUES ("${user_name}", "${email}", "${password}", "${image}")`)
.then((results) => {
res.send("Registration successful")
if(err)throw err;
})
.catch((error) => {
console.log(error)
})
})
}
})
.catch((error) => {
console.log(error)
})
} else {
res.send("Enter email")
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

任何帮助都将不胜感激。提前谢谢。

我假设您正在使用form上传文件。如果,那么…

前端片段:

<form action="/register" method="post" enctype="multipart/form-data">
<input type="file" name="image" />
</form>

让multer正确上传文件:

const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './client/public/img')
},
filename: function (req, file, cb) {
if(file.mimetype == "image/jpeg" || file.mimetype == "image/png") {
cb(null, file.fieldname + '-' + Date.now());
} 
else {
cb(new MulterError('LIMIT_UNEXPECTED_FILE', file.fieldname));
}
});

const upload = multer({ storage: storage });

通过如下调用上传来处理Express中间件内部的错误。。。

app.post('/profile', function (req, res) {
upload(req, res, function (err) {
if (err instanceof multer.MulterError) {
// A Multer error occurred when uploading.
} else if (err) {
// An unknown error occurred when uploading.
}

// Everything went fine.
})
})

相关内容

  • 没有找到相关文章

最新更新