我在用Multer上传文件。一切都很好,但filefilter不工作。我不知道我错在哪里。
路由.js

var multer = require('multer');
// Multer diskStorage setting
var storage = multer.diskStorage({
  destination: function (req, file, callback) {
    callback(null, './uploads/');
  },
  filFilter: function (req, file, cb) {
    var typeArray = file.mimetype.split('/');
    var fileType = typeArray[1];
    if (fileType == 'jpg' || fileType == 'png') {
      cb(null, true);
    } else {
      cb(null, false)
    }
  },
  filename: function (req, file, callback) {
    callback(null, file.fieldname + '_' + Date.now() + '.jpg');
  }
});

var upload = multer({ storage: storage });
router.post('/add', upload.single('photo'), function(req, res){

    console.log('uploaded');
}

Multer也会上传不允许的文件。知道吗?

最佳答案

fileFilter必须在multer的options object中定义,而不是在diskstorage中定义

const uploadFilter = function(req, file, cb) {
    // filter rules here
}

const upload = multer({
    storage: storage,
    fileFilter: uploadFilter,
});

关于node.js - multer:fileFilter不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48744421/

10-12 15:39