I am implementing 'image uploading' functionality using multer, angular 6, ng2-file uploader, and nodeJs. I want to upload only '.png' & '.jpeg' file types. Here is the backend.
/* UPLOAD image file */
router.post('/upload',function(req,res){
var storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, DIR);
},
filename: (req, file, cb) => {
cb(null,file.fieldname+".jpg");
},
});
var upload = multer({storage: storage,limits: { fileSize: 524288 }}).single('photo');
upload(req,res,function(err) {
if(err) {
console.log(err);
return res.end("Error uploading file.");
} else {
res.end("File has been uploaded");
}
});
});
I want to upload .png and .jpg files. So I restricted other file extensions from the frontend.
<input type="file" #fileInput name="photo" ng2FileSelect [uploader]="uploader" (change)="handleFileInput($event.target.files)"
accept="image/jpeg, image/png" />
After restricting other file types first it only shows the required files in the file explorer (customized file types). But there is an option to select all files in the file explorer. Then it shows other file types too. Then it allows uploading other file types as well. What I want is to restrict uploading file types other than .png and .jpeg. Is there a proper way to achieve it.