在我的项目中,我必须使用 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
]);
你能帮我解决这个问题吗
最佳答案
尝试将 file/psd
更改为其中的一些(第一个应该可以):
image/vnd.adobe.photoshop
application/x-photoshop
application/photoshop
application/psd
image/psd
关于javascript - Multer Node.js 的文件上传 MIME 类型问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67326727/