multer无法上传文件

时间:2019-03-27 16:24:24

标签: node.js multer

我正在使用multer,并且已经包含了所有必要的信息,例如在表单标签中,即enctype =“ multipart / form-data”。我用过diskStorage和Filefilter。我仍然无法上传文件。它给了我req.file = undefined。

我搜索了stackoverflow上的各种链接,但没有任何帮助。

  <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)=>{
                  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,fileFilter:fileFilter1, limits: 
               {fileSize:5*1024*1024}}).single('adfile'));


   app.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);
                 }
         })

0 个答案:

没有答案