如何在multer中检查文件类型
how to check file type in multer
我正在用 React 做一个项目,运行 遇到了一个让我难过的问题。
我想在 cloudinary 上上传我的文件,也想检查我的文件类型和 mime 类型,但遇到这个错误这是我的代码
所以这是我的代码,我无法过滤掉文件类型。
如果有人有解决办法,如果你能帮助我就太好了
const multer = require("multer");
const DatauriParser = require("datauri/parser");
const path = require("path");
const fs = require("fs");
const dontenv = require("dotenv");
dontenv.config({ path: "config.env" });
let storage;
let dataUri;
const parser = new DatauriParser();
if (process.env.STORAGE_TYPE == "cloud") {
storage = multer.memoryStorage();
dataUri = (req) =>
parser.format(
path.extname(req.file.originalname).toString(),
req.file.buffer
);
} else {
dataUri = {};
storage = multer.diskStorage({
destination: function (req, file, cb) {
var directory = process.env.STORAGE;
if (!fs.existsSync(directory)) {
fs.mkdirSync(directory);
}
cb(null, directory);
},
filename: function (req, file, cb) {
cb(
null,
file.fieldname +
"-" +
Date.now() +
"." +
file.originalname.split(".")[1]
);
},
fileFilter: function (req, file, cb) {
checkFileType(file, cb);
},
});
function checkFileType(file, cb) {
// Allowed ext
const filetypes = /jpeg|jpg|png|gif/;
// Check ext
const extname = filetypes.test(
path.extname(req.file.originalname).toString()
);
// Check mime
const mimetype = filetypes.test(file.mimetype);
if (mimetype && extname) {
return cb(null, true);
} else {
cb("Error: Images Only!");
}
}
// to Check File TYpe
}
const multerUploads = multer({ storage: storage }).single("imageUrl");
module.exports = { multerUploads, dataUri };
我不知道为什么会这样,如果有人经历过,我将不胜感激。
首先fileFilter
不是diskStorage
的key,它是multer本身的key。此外,您已经在 else
中声明了函数。所以使用 multer 检查文件类型应该是这样的:
const multer = require("multer");
const DatauriParser = require("datauri/parser");
const path = require("path");
const fs = require("fs");
const dontenv = require("dotenv");
dontenv.config({ path: "config.env" });
let storage;
let dataUri;
const parser = new DatauriParser();
if (process.env.STORAGE_TYPE == "cloud") {
storage = multer.memoryStorage();
dataUri = (req) =>
parser.format(
path.extname(req.file.originalname).toString(),
req.file.buffer
);
} else {
dataUri = {};
storage = multer.diskStorage({
destination: function (req, file, cb) {
var directory = process.env.STORAGE;
if (!fs.existsSync(directory)) {
fs.mkdirSync(directory);
}
cb(null, directory);
},
filename: function (req, file, cb) {
cb(
null,
file.fieldname +
"-" +
Date.now() +
"." +
file.originalname.split(".")[1]
);
}
});
// to Check File TYpe
}
function checkFileType(file, cb) {
// Allowed ext
const filetypes = /jpeg|jpg|png|gif/;
// Check ext
const extname = filetypes.test(
path.extname(file.originalname).toString()
);
// Check mime
const mimetype = filetypes.test(file.mimetype);
if (mimetype && extname) {
return cb(null, true);
} else {
cb("Error: Images Only!");
}
}
const multerUploads = multer({
storage: storage,
fileFilter: function (req, file, cb) {
checkFileType(file, cb);
},
}).single("imageUrl");
module.exports = { multerUploads, dataUri };
我正在用 React 做一个项目,运行 遇到了一个让我难过的问题。
我想在 cloudinary 上上传我的文件,也想检查我的文件类型和 mime 类型,但遇到这个错误这是我的代码
所以这是我的代码,我无法过滤掉文件类型。
如果有人有解决办法,如果你能帮助我就太好了
const multer = require("multer");
const DatauriParser = require("datauri/parser");
const path = require("path");
const fs = require("fs");
const dontenv = require("dotenv");
dontenv.config({ path: "config.env" });
let storage;
let dataUri;
const parser = new DatauriParser();
if (process.env.STORAGE_TYPE == "cloud") {
storage = multer.memoryStorage();
dataUri = (req) =>
parser.format(
path.extname(req.file.originalname).toString(),
req.file.buffer
);
} else {
dataUri = {};
storage = multer.diskStorage({
destination: function (req, file, cb) {
var directory = process.env.STORAGE;
if (!fs.existsSync(directory)) {
fs.mkdirSync(directory);
}
cb(null, directory);
},
filename: function (req, file, cb) {
cb(
null,
file.fieldname +
"-" +
Date.now() +
"." +
file.originalname.split(".")[1]
);
},
fileFilter: function (req, file, cb) {
checkFileType(file, cb);
},
});
function checkFileType(file, cb) {
// Allowed ext
const filetypes = /jpeg|jpg|png|gif/;
// Check ext
const extname = filetypes.test(
path.extname(req.file.originalname).toString()
);
// Check mime
const mimetype = filetypes.test(file.mimetype);
if (mimetype && extname) {
return cb(null, true);
} else {
cb("Error: Images Only!");
}
}
// to Check File TYpe
}
const multerUploads = multer({ storage: storage }).single("imageUrl");
module.exports = { multerUploads, dataUri };
我不知道为什么会这样,如果有人经历过,我将不胜感激。
首先fileFilter
不是diskStorage
的key,它是multer本身的key。此外,您已经在 else
中声明了函数。所以使用 multer 检查文件类型应该是这样的:
const multer = require("multer");
const DatauriParser = require("datauri/parser");
const path = require("path");
const fs = require("fs");
const dontenv = require("dotenv");
dontenv.config({ path: "config.env" });
let storage;
let dataUri;
const parser = new DatauriParser();
if (process.env.STORAGE_TYPE == "cloud") {
storage = multer.memoryStorage();
dataUri = (req) =>
parser.format(
path.extname(req.file.originalname).toString(),
req.file.buffer
);
} else {
dataUri = {};
storage = multer.diskStorage({
destination: function (req, file, cb) {
var directory = process.env.STORAGE;
if (!fs.existsSync(directory)) {
fs.mkdirSync(directory);
}
cb(null, directory);
},
filename: function (req, file, cb) {
cb(
null,
file.fieldname +
"-" +
Date.now() +
"." +
file.originalname.split(".")[1]
);
}
});
// to Check File TYpe
}
function checkFileType(file, cb) {
// Allowed ext
const filetypes = /jpeg|jpg|png|gif/;
// Check ext
const extname = filetypes.test(
path.extname(file.originalname).toString()
);
// Check mime
const mimetype = filetypes.test(file.mimetype);
if (mimetype && extname) {
return cb(null, true);
} else {
cb("Error: Images Only!");
}
}
const multerUploads = multer({
storage: storage,
fileFilter: function (req, file, cb) {
checkFileType(file, cb);
},
}).single("imageUrl");
module.exports = { multerUploads, dataUri };