I am working with Nodejs and using expressjs framework,Right now i am trying to upload image using Api but
i want two things
1) if image extension is not "jpg,png,gif" then error message should display
Here is my current code
var storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, './uploads');
},
filename: function (req, file, callback) {
var sizeOf = require('image-size');
var sizeOf = require('image-size');
const originalname = file.originalname;
const fileExt = originalname.split(".").pop();
const excatName = originalname.split(".").slice(0, -1).join(".");
console.log("mimetype is " + file.mimetype); // GETTING FILE EXTENSION NAME
callback(null, excatName + "-" + Date.now().toString() + "." + fileExt);
},
});
var upload = multer({ storage: storage }).single('userPhoto');
const uploadavatar = function (req, res) {
upload(req,res,function(err) {
if(err) {
return res.end("Error uploading file." + err);
}
res.end("File is uploaded");
});
};
2
Answers
you can pass a filter function to multer params object and with help of filter you can exclude mimetype and a create a error : –