代码之家  ›  专栏  ›  技术社区  ›  Hysii

基于MIME类型的多文件过滤

  •  0
  • Hysii  · 技术社区  · 7 年前

    我有下面的代码。我想使用multer只接受docx的MIME类型的文档。否则,我想产生一个错误。我也计划在前端进行审查,但出于安全考虑,我也希望在这里实现。下面的代码返回一个错误,有人能告诉我哪里错了吗?

    const multer = require('multer')
    const fs = require('fs')
    
    const upload = multer({
      dest: './upload',
      fileFilter: function (req, file, cb) {
          if (req.file.mimetype != 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') {
              return cb(new Error('Wrong file type'))
          }
          cb(null,true)
      }
    }).single('file');
    
    app.post('/upload', upload, function(req, res) {
    
      console.log(req.file);
    };
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   yotke    7 年前

    当我使用 file.mimetype 而不是 req.file.mimetype .

    fileFilter: function (req, file, cb) {
        if (file.mimetype != 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') {
            return cb(new Error('Wrong file type'));
        }
        cb(null, true)
      }