如何修复Node.js Express服务器中的“ MulterError:意外字段”?

时间:2019-03-28 13:24:13

标签: javascript node.js multer

我正在设置服务器以从客户端上载zip文件。服务器运行express和multer来执行此操作。上载文件时,服务器会引发“ MulterError:意外字段”错误,我无法查明是什么原因造成的。

我尝试了png图片,效果很好。但是对于zip文件,它根本不起作用。

const multerConfig = {
    //specify diskStorage (another option is memory)
    storage: multer.diskStorage({
      //specify destination
      destination: function(req, file, next){
        next(null, './public/zip-storage');
      },

      //specify the filename to be unique
      filename: function(req, file, next){
        console.log(file);
        const ext = file.mimetype.split('/')[1];
        //set the file fieldname to a unique name containing the original name, current datetime and the extension.
        next(null, file.fieldname + '-' + Date.now() + '.'+ext);
      }
    }),

    // filter out and prevent non-image files.
    fileFilter: function(req, file, next){
          if(!file){
            next();
          }
        // only permit zip mimetypes
        const zip = file.mimetype.startsWith('application');
        if(zip){
          console.log('zip uploaded');
          next(null, true);
        }else{
          console.log("file not supported")
          errorReq = true;
          return next();
        }
    }
  };


  /* ROUTES
  **********/
  app.get('/', function(req, res){
    res.render('index.html');
  });

  var errorDetection = function(){
    if(!errorReq){
      errorReq = false;
      return('complete.html');
    } else{
      errorReq = false;
      return('errorupload.html');
    }
  }

  app.post('/upload', multer(multerConfig).single('photo'),function(req, res){
      //Here is where I could add functions to then get the url of the new photo
      //And relocate that to a cloud storage solution with a callback containing its new url
      //then ideally loading that into your database solution.   Use case - user uploading an avatar...
      res.redirect('complete.html');
  }

);

  // RUN SERVER
  app.listen(port,function(){
    console.log(`Server listening on port ${port}`);
  });

这是错误:

MulterError: Unexpected field
    at wrappedFileFilter (/home/axentiva-miguel/Documentos/ServerAPP/node_modules/multer/index.js:40:19)
    at Busboy.<anonymous> (/home/axentiva-miguel/Documentos/ServerAPP/node_modules/multer/lib/make-middleware.js:114:7)
    at emitMany (events.js:147:13)
    at Busboy.emit (events.js:224:7)
    at Busboy.emit (/home/axentiva-miguel/Documentos/ServerAPP/node_modules/busboy/lib/main.js:38:33)
    at PartStream.<anonymous> (/home/axentiva-miguel/Documentos/ServerAPP/node_modules/busboy/lib/types/multipart.js:213:13)
    at emitOne (events.js:116:13)
    at PartStream.emit (events.js:211:7)
    at HeaderParser.<anonymous> (/home/axentiva-miguel/Documentos/ServerAPP/node_modules/dicer/lib/Dicer.js:51:16)
    at emitOne (events.js:116:13)

1 个答案:

答案 0 :(得分:0)

输入

app.post('/upload', multer(multerConfig).single('photo'),function(req, res){
      //Here is where I could add functions to then get the url of the new photo
      //And relocate that to a cloud storage solution with a callback containing its new url
      //then ideally loading that into your database solution.   Use case - user uploading an avatar...
      res.redirect('complete.html');
  }

当我以html格式发送时,我将值single('photo')放在了其中,其中的'zip'值就在其中。更改使代码按预期工作了:)