我有下面的代码。我想使用multer仅接受MIME类型为docx的文档。否则,我想产生一个错误。我也计划在前端进行审核,但出于安全考虑,也希望在此处进行审核。以下代码返回错误,有人可以告诉我我错了吗?
const multer = require('multer')
const fs = require('fs')
const upload = multer({
dest: './upload',
fileFilter: function (req, file, cb) {
if (req.file.mimetype != 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') {
return cb(new Error('Wrong file type'))
}
cb(null,true)
}
}).single('file');
app.post('/upload', upload, function(req, res) {
console.log(req.file);
};
答案 0 :(得分:1)
当我使用 const request = require('request');
request('https://lc-txtconnect5.globe.com.ph/inbox?token=3a64892d865cd4e386b161767245fa0afc808c63957a7e495f65829a56b96b03&txtid=51talk', { json: true }, (err, res, body) => {
if (err) { return console.log(err); }
var messages = body.response.response;
console.log(messages);
messages.forEach(function(messages) {
var tblmessage = messages.message;
console.log(tblmessage);
});
});
而不是file.mimetype
时,它对我有用。
req.file.mimetype
希望有帮助。
答案 1 :(得分:0)
You can try following code snippet to accept only file type of docx :
const filefilter = (req, file, cb) => {
if (
file.mimetype === "application/msword" ||
file.mimetype ===
"application/vnd.openxmlformatsofficedocument.wordprocessingml.document"
) {
cb(null, true);
} else {
cb(null, false);
}
};