javascript - 如何修复nodejs Express服务器中的 "MulterError: Unexpected field"?

标签 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)

最佳答案

在代码中

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' 值。改变这一点使代码按预期工作:)

关于javascript - 如何修复nodejs Express服务器中的 "MulterError: Unexpected field"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55398763/

相关文章:

html - 在 Jade Pug 的循环中使用 CSS id 变量

javascript - 尝试使用 multer node.js 上传时出现 404 错误

javascript - 发送 API 响应后将 NodeJ 中的路由与值链接起来

javascript - NodeJS + Socket.IO : Problem accessing room variables

javascript - 用于创建模式对话框的 JQuery 编码可以在 jsfiddle 中工作,但不能在本地 html 文件中工作

javascript - 没有 node.js 服务器的客户端 socket.io

javascript - 是否有任何选项可以减小图像大小并在具有多种功能的nodejs中调整图像大小?

javascript - 为什么 SVG 上的文本不更新,而 div 上的文本却更新?

javascript - Uncaught ReferenceError 。移相器.io

javascript - 如何在 Nodejs 中使用 twitter api 发布表情符号