【问题标题】:file upload mime type issue with Multer Node.jsMulter Node.js 的文件上传 MIME 类型问题
【发布时间】:2021-04-30 00:33:03
【问题描述】:

在我的项目中,我必须使用 Multer 上传一个 Photoshop 文件,但是当我输入 mime 类型的 photoshop、wave 和 mp3 文件时,我收到了这个错误:

Error: Mime type invalide
at DiskStorage.destination [as getDestination] (C:\Users\21653\Desktop\vagaBeats\back\src\midlleware\artworkImageUploader.js:14:21)
at DiskStorage._handleFile (C:\Users\21653\Desktop\vagaBeats\back\node_modules\multer\storage\disk.js:31:8)
at C:\Users\21653\Desktop\vagaBeats\back\node_modules\multer\lib\make-middleware.js:144:17
at allowAll (C:\Users\21653\Desktop\vagaBeats\back\node_modules\multer\index.js:8:3)
at wrappedFileFilter (C:\Users\21653\Desktop\vagaBeats\back\node_modules\multer\index.js:44:7)
at Busboy.<anonymous> (C:\Users\21653\Desktop\vagaBeats\back\node_modules\multer\lib\make-middleware.js:114:7)
at Busboy.emit (events.js:315:20)
at Busboy.emit (C:\Users\21653\Desktop\vagaBeats\back\node_modules\busboy\lib\main.js:38:33)
at PartStream.<anonymous> (C:\Users\21653\Desktop\vagaBeats\back\node_modules\busboy\lib\types\multipart.js:213:13)
at PartStream.emit (events.js:315:20)

这是我的代码:

const multer = require("multer");
const path = require('path');

const MIME_TYPE_MAP = {
    "image/png": "png",
    "image/jpeg": "jpg",
    "image/jpg": "jpg",
    "file/psd": "psd" // i got a problem with this MIME TYPE
};

const storage = multer.diskStorage({
    destination: (req, file, cb) => {
        const isValid = MIME_TYPE_MAP[file.mimetype];
        let error = new Error("Mime type invalide");
        if (isValid) {
            error = null;
        }
        cb(error, path.join(__dirname, '../../', 'images/artworkImages'));
    },
    filename: (req, file, cb) => {
        const name = file.originalname
            .toLowerCase()
            .split(" ")
            .join("-");
        console.log(file)
        const ext = MIME_TYPE_MAP[file.mimetype];
        cb(null, name + "-" + Date.now() + "." + ext);
    }
});

module.exports = multer({ storage: storage }).fields([
{ name: "Image"},
{name:"untagedartwork"},
{name:"psd_file"} //this is the file that i want to upload it to the server
]);
你能帮我解决这个问题吗

【问题讨论】:

  • 你可以在const isValid = MIME_TYPE_MAP[file.mimetype];这一行上面放一个console.log(file.mimetype)来查看值。

标签: javascript node.js express sequelize.js multer


【解决方案1】:

尝试将file/psd 更改为其中的一些(第一个应该可以):

image/vnd.adobe.photoshop

application/x-photoshop

application/photoshop

application/psd

image/psd

【讨论】:

  • 感谢您的帮助,第一个工作没有任何问题
  • 我很高兴听到这个消息。如果对您有帮助,请考虑投票并批准我的回答。
猜你喜欢
  • 2015-01-11
  • 2021-06-17
  • 2016-03-03
  • 1970-01-01
  • 2010-10-16
  • 2016-03-06
  • 2017-10-20
  • 2011-06-09
  • 2013-10-20
相关资源
最近更新 更多