expressjs / multer

Node.js middleware for handling `multipart/form-data`.
MIT License
11.61k stars 1.06k forks source link

Multer req.file is undefined #725

Open karandutt01 opened 5 years ago

karandutt01 commented 5 years ago
       <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;
karandutt01 commented 5 years ago

@LinusU I am using multer but it is giving me req.file undefined everytime. Please help me out what am I doing wrong?

LinusU commented 5 years ago

The method attribute here should probably be POST, GET requests can't have a request body afaik...

jonchurch commented 4 years ago

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.