Open wd603546401 opened 5 years ago
I have not been able to reproduce this. Could you post a minimal test case that shows this problem?
I have not been able to reproduce this. Could you post a minimal test case that shows this problem? here is my code: `var express = require('express'); const router = express.Router(); var multer = require('multer'); var fs = require('fs'); var path = require('path'); const log = require(process.cwd() + '/common/log4js.js').log; const conf = require(process.cwd() + '/config/index.js');
const subPathList = ['default', 'idcard', 'logo']; const supportFormts = ['jpg', 'png', 'gif'];
var fileName = ''; var subPath = '';
function getFileSubPath(params) { let index = subPathList.indexOf(params.type); return index > -1 ? subPathList[index] : subPathList[0]; }
function mkdirsSync(dirname) { if (fs.existsSync(dirname)) { return true; } else { if (mkdirsSync(path.dirname(dirname))) { fs.mkdirSync(dirname); return true; } } }
var storage = multer.diskStorage({
destination: function (req, file, cb) {
const data = new Date();
let month = data.getMonth() + 1;
let date = data.getDate();
month = month < 10 ? 0${month}
: month;
date = date < 10 ? 0${date}
: date;
let dataLayer = ${data.getFullYear()}${month}${date}
;
subPath = ${getFileSubPath(req.body)}/${dataLayer}
;
const imgpath = path.resolve(conf.uploadroot, subPath);
req.session.imgpath = imgpath;
try {
if (!fs.existsSync(imgpath)) {
mkdirsSync(imgpath);
}
} catch (e) {
log.error(e);
}
cb(null, path.resolve(imgpath));
},
filename: function (req, file, cb) {
fileName = Date.now() + '_' + file.originalname;
req.session.fileName = fileName;
cb(null, fileName);
}
});
/**
var uploadMulter = multer({ storage: storage, fileFilter: fileFilter, limits: { fieldSize: 1024 1024 5, fileSize: 1024 1024 5 } }).single('fileName');
router.post('/', function (req, res, next) { req.session.fileName = ''; // form表单方式提交没问题,but postman失败 uploadMulter(req, res, (err) => { if (err || !req.session.fileName) { log.error(req.session.user.userName, '文件上传失败', err); res.send({ status: 1, msg: '上传失败' }); return; } let fileName = req.session.fileName; log.info(req.session.user.userName, '文件上传成功', subPath + '/' + fileName); res.send({ status: 0, msg: '成功', result: subPath + '/' + fileName }); }); });
module.exports = router; ` I have solved this problem, is ‘fileFilter’ settings,in iphone device the photo format is jpeg and my settings does not include this format,so。。。,but another problem is: why the multer has not throw any error?
@wd603546401 fileFilter
is designed to silently ignore the files which are not supported by the fileFilter function without throwing the error ( which lets the server crash). Since throwing error makes server crash, I guess that is not what you want.
If you still want to throw error, you can make use of following code in fileFilter
function.
var filefilter = function(req, file, cb){
let suffix = file.originalname.slice(file.originalname.lastIndexOf('.') + 1)
if (!suffix || Formats.indexOf(suffix.toLowerCase()) === -1) {
var err = new Error('File format not supported')
cb(null, false)
throw(err)
}
cb(null, true)
}
As title, in iphone device any browser(safair or UC browers ...) upload file can not be received by multer ,I use wireshark to catch the network request and I found that the browers has send file already ,but at server the mutler plugin can not receive and without any error, why? my email address : wd603546401@gmail.com