我部署了一个基于Web的应用程序,但该multer上传不起作用。它确实可以在本地主机上工作,没有错误,但是文件没有保存在我的控制器中。这是我的代码:
const multer = require("multer");
const imageFilter = (req, file, cb) => {
if (file.mimetype.startsWith("image")) {
cb(null, true);
} else {
cb("Please upload only images.", false);
}
};
var storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, __basedir + "/resources/static/assets/uploads/");
},
filename: (req, file, cb) => {
cb(null, `${file.originalname}`);
},
});
var uploadFile = multer({ storage: storage, fileFilter: imageFilter });
module.exports = uploadFile;
答案 0 :(得分:0)
发生的情况是,__basedir
表达式将导致“ Uncaught ReferenceError:未定义__basedir”错误,但在代码中已被静默吃掉。为了使错误正确传播,请尝试在有问题的行周围添加try-catch块:
const storage = multer.diskStorage({
destination: (req, file, cb) => {
try {
cb(null, __basedir + "/resources/static/assets/uploads/");
} catch (e) {
cb(e);
}
},
filename: (req, file, cb) => {
cb(null, `${file.originalname}`);
}
});