As title says.
正如标题所说。
I looked everywhere, couldn't find an answer.
我到处找,找不到答案。
CODE:
代码:
var upload = multer({dest:"./public/images/uploads/", limits: {fileSize: 250000}}).single("image");
PROBLEM
问题
This does not prevent me from uploading a pdf if I choose to do so.
这并不妨碍我上传pdf,如果我选择这样做。
1 个解决方案
#1
2
The docs state you should use fileFilter to possibly skip files for upload.
fileFilter (https://github.com/expressjs/multer#filefilter)
文档说明应该使用fileFilter来跳过文件上传。fileFilter(https://github.com/expressjs/multer # fileFilter)
Set this to a function to control which files should be uploaded and which should be skipped. The function should look like this:
function fileFilter (req, file, cb) {
// The function should call `cb` with a boolean
// to indicate if the file should be accepted
// To reject this file pass `false`, like so:
cb(null, false)
// To accept the file pass `true`, like so:
cb(null, true)
// You can always pass an error if something goes wrong:
cb(new Error('I don\'t have a clue!'))
}
From the docs I would assume that the passed in file
has a property mimetype
(https://github.com/expressjs/multer#api). This could be a good hint for the decision if you want to skip.
从文档中,我假设传递的文件有一个属性mimetype (https://github.com/expressjs/multer#api)。如果你想跳过,这可能是一个很好的提示。
EDIT: this GH issues (https://github.com/expressjs/multer/issues/114#issuecomment-231591339) contains a good example for the usage. It's important to not only look at the file extension because this can easily be renamed but also take the mime-type into account.
编辑:这个GH问题(https://github.com/expressjs/multer/issues/114# issues -231591339)包含一个很好的用法示例。重要的是,不仅要查看文件扩展名,因为它很容易被重命名,而且还可以考虑mime类型。
const path = require('path');
multer({
fileFilter: function (req, file, cb) {
var filetypes = /jpeg|jpg/;
var mimetype = filetypes.test(file.mimetype);
var extname = filetypes.test(path.extname(file.originalname).toLowerCase());
if (mimetype && extname) {
return cb(null, true);
}
cb("Error: File upload only supports the following filetypes - " + filetypes);
}
});
HTH
HTH
#1
2
The docs state you should use fileFilter to possibly skip files for upload.
fileFilter (https://github.com/expressjs/multer#filefilter)
文档说明应该使用fileFilter来跳过文件上传。fileFilter(https://github.com/expressjs/multer # fileFilter)
Set this to a function to control which files should be uploaded and which should be skipped. The function should look like this:
function fileFilter (req, file, cb) {
// The function should call `cb` with a boolean
// to indicate if the file should be accepted
// To reject this file pass `false`, like so:
cb(null, false)
// To accept the file pass `true`, like so:
cb(null, true)
// You can always pass an error if something goes wrong:
cb(new Error('I don\'t have a clue!'))
}
From the docs I would assume that the passed in file
has a property mimetype
(https://github.com/expressjs/multer#api). This could be a good hint for the decision if you want to skip.
从文档中,我假设传递的文件有一个属性mimetype (https://github.com/expressjs/multer#api)。如果你想跳过,这可能是一个很好的提示。
EDIT: this GH issues (https://github.com/expressjs/multer/issues/114#issuecomment-231591339) contains a good example for the usage. It's important to not only look at the file extension because this can easily be renamed but also take the mime-type into account.
编辑:这个GH问题(https://github.com/expressjs/multer/issues/114# issues -231591339)包含一个很好的用法示例。重要的是,不仅要查看文件扩展名,因为它很容易被重命名,而且还可以考虑mime类型。
const path = require('path');
multer({
fileFilter: function (req, file, cb) {
var filetypes = /jpeg|jpg/;
var mimetype = filetypes.test(file.mimetype);
var extname = filetypes.test(path.extname(file.originalname).toLowerCase());
if (mimetype && extname) {
return cb(null, true);
}
cb("Error: File upload only supports the following filetypes - " + filetypes);
}
});
HTH
HTH