我在使用nodejs上传cloudinary视频时遇到错误。我得到错误{ message: 'Invalid image file', name: 'Error', http_code: 400 }
,即使我上传.mp4文件或视频文件,我仍然得到错误。我尝试与图像配置上传图像,它是成功地得到上传。我正在使用multer库这一点
我尝试上传不同大小的视频和不同的扩展名,我已经过滤文件与mimetype“视频/mp4”,我试图复制和粘贴教程的代码,但仍然没有运气,得到错误,我试图搜索网页和chatgpt,但没有运气。希望好心人和栈溢出的成员帮助我。
谢谢你提前:)
我的multer配置文件:
const multer = require("multer");
const storage = multer.diskStorage({
filename: (req, file, cb) => {
const fileExt = file.originalname.split(".").pop();
const filename = `${new Date().getTime()}.${fileExt}`;
cb(null, filename);
},
});
const imageFileFilter = (req, file, cb) => {
if (!file.mimetype.startsWith("image")) {
cb("Supports only image files!", false);
}
cb(null, true);
};
const videoFileFilter=(req, file, cb) => {
// if (!file.mimetype.startsWith("video")) {
// cb("Supports only video files!", false);
// }
if(file.mimetype!=="video/mp4"){
cb("Supports only video files!", false);
}
cb(null, true);
};
exports.uploadImage = multer({ storage, fileFilter:imageFileFilter });
exports.uploadVideo = multer({ storage, fileFilter:videoFileFilter});
my router file :
`router.post('/upload-trailer',uploadVideo.single('trailer'),uploadTrailer);`
my controller file :
`exports.uploadTrailer = async (req, res,next) => {
const { file } = req;
console.log(file)
try{
if (!file) return sendError(res, "Video file is missing!");
const { secure_url: url, public_id } = await cloudinary.uploader.upload(
file.path,
{
resource_type: "video",
}
);
res.status(201).json({ url, public_id });
}
catch(error){
console.log(error)
next(error)
}
};`
file or video info
connected to database successfully
`{
fieldname: 'trailer',
originalname: 'pexels-pressmaster-9034146-1920x1080-25fps.mp4',
encoding: '7bit',
mimetype: 'video/mp4',
destination: 'C:\\Users\\Mahir\\AppData\\Local\\Temp',
filename: '1685539421329.mp4',
path: 'C:\\Users\\Mahir\\AppData\\Local\\Temp\\1685539421329.mp4',
size: 6087806
}`
attaching postman截图
1条答案
按热度按时间wmtdaxz31#
根据您收到的错误响应,看起来您正在将文件作为图像上传。你能不能试着把它做成一段视频,然后再看看你的条件句?