Multer fileFilter无效文件上传导致客户端页面卡住加载不完整的html文件

问题描述 投票:0回答:0

无论出于何种原因,当我尝试上传不属于可接受文件类型的文件时,res.render 发送的视图不完整。只有大约 90% 的 html 被发送,客户端页面只显示“正在连接...”,直到我停止服务器。但是,一旦我停止服务器,页面就会完全加载。我做错了什么?

Multer 上传

const upload = multer({
    storage: storage,
    fileFilter: (req, file, callback) => {
        const filetypes = /jpeg|jpg|png|gif/;
        const extension = filetypes.test(path.extname(file.originalname).toLowerCase());
        const mimetype = filetypes.test(file.mimetype);
        if (!extension || !mimetype) return callback("Invalid file format provided.\nAllowed types are .jpeg, .jpg, .png, & .gif.");
        callback(null, true);
    }
}).single("selectImage");

邮寄处理

app.post("/event", (req, res) => {
    try {
        upload(req, res, (fail) => {
            console.log(fail);
            res.render("event", {
                message: {
                    type: "success",
                    message: "Successfully updated event information."
                },
                event: event
            });
        })
    } catch (e) {
        res.status(500).json("Internal server error, please try refreshing.");
        console.log("EVENT POST ERROR: " + e);
    }
})

上传接受的文件类型后发送的页面

上传无效文件类型后发送的页面(页面卡住加载)

依赖关系

"dependencies": {
    "bcryptjs": "^2.4.3",
    "cookie-parser": "^1.4.6",
    "cors": "^2.8.5",
    "dotenv": "^16.0.3",
    "ejs": "^3.1.8",
    "express": "^4.18.2",
    "jsonwebtoken": "^9.0.0",
    "multer": "^1.4.5-lts.1"
}
node.js express file-upload multer
© www.soinside.com 2019 - 2024. All rights reserved.