首页 > 解决方案 > express js multer upload.single 不工作

问题描述

Multer 上传文件正常,但突然相同的代码没有在请求中获取文件对象。就像 multer 被完全忽略了一样。在请求中检查文件时,req.file 未定义。

function multerStore() {
return multer.diskStorage({
    destination: function (req, file, cb) {
        console.log(req);
        execHomeDir().then(resp => {
            const nameo = resp.replace("\n", "");
            const folderPath = nameo + attachmentTemp;
            createFolder(folderPath).then(resp=>{
                cb(null, folderPath);
            }).catch(ree=>{
                cb(null, folderPath);
            });
        }).catch(ree => {
            console.error('Error Multer');
            console.error(ree);
            const nameo = ree.replace("\n", "");
            const folderPath = nameo + attachmentTemp;
            createFolder(folderPath).then(resp=>{
                cb(null, folderPath);
            }).catch(ree=>{
                cb(null, folderPath);
            });
        });
    },
    filename: function (req, file, cb) {
        console.log(req);
        console.log('Incoming file'+file.filename);
        const name = setFileName('mexuser') + file.originalname;
        cb(null, name);
    }
});}
function multerOps() {
return multer({ storage: multerStore(), limits: fileLimiter });}const attachOps = multerOps('attachment', attachmentTemp);fileRouter.post('/:type', attachOps.single('attachment'),files.upload);

标签: javascriptnode.jsexpressmultipartform-datamulter

解决方案


推荐阅读