<body>
<form action="fileupload" method="GET" enctype="multipart/form-data">
Select images:<input type="file" name="adfile">
<button type="submit">Upload your files</button>
</form>
</body>
const storageFile=multer.diskStorage({
destination:(req,file,next)=>{
console.log('file destination', file);
next(null,'./images/');
},
filename:(req,file,next)=>{
console.log("file is::::", file);
next(null,file.fieldname + path.extname(file.originalname));
}
}),
fileFilter1=(req,file,next)=>{
console.log("FILE IS ",file);
let err=null;
var isMimeMatch;
if (file.mimetype == 'image/jpg' || file.mimetype == 'image/jpeg' || file.mimetype ==
'image/png') {
isMimeMatch = true;
next(err,isMimeMatch);
}
else {
isMimeMatch = false;
console.log("Mime not Match ",error, "isMimeMatch ",isMimeMatch);
next(error,isMimeMatch);
}
}
app.use(multer({storage:storageFile,limits:
{fileSize:5*1024*1024},fileFilter:fileFilter1}).single('adfile'));
app.use('/',require('./Routes/uploadRoute'));
const uploadRoute=require('express').Router();
uploadRoute.get('/fileupload',(req,res)=>{
res.render('fileupload');
var fileName=req.file;
if(!fileName) {
console.log('kkkkkkkk');
console.log('File is not uploaded');
}
else {
console.log('nnnnnnnn');
console.log(fileName);
}
})
module.exports=uploadRoute;
@LinusU I am using multer but it is giving me req.file undefined everytime. Please help me out what am I doing wrong?
The method attribute here should probably be POST, GET requests can't have a request body afaik...
Important to note also that with that form, it will redirect you to the url. I got this to work for me where it did indeed upload the file with a GET request. But then when the page was reloaded after submitting, req.file was undefined because I was just loading the page, not submitting a form to it.
Most helpful comment
The
methodattribute here should probably bePOST,GETrequests can't have a request body afaik...