我正在尝试使用multer上传多个文件(分别在单独的文件夹中的视频和图像)

时间:2020-06-29 09:12:33

标签: node.js express multer cloudinary

云配置 配置正常,我在管理控制台中检查了上传的视频是否存在

但是该混合器不适用于视频

const crypto = require('crypto');
const cloudinary = require('cloudinary');
const cloudinaryStorage = require('multer-storage-cloudinary');
const storage1 = cloudinaryStorage({
    cloudinary,
    folder: 'bat1',
    allowedFormats: ['jpeg', 'jpg', 'png'],
    filename: function (req, file, cb) {
        let buf = crypto.randomBytes(16); //generating random 16 Bytes
        buf = buf.toString('hex'); //Converting to hex will give 32 CHARACTERS & converting to string
        let uniqFileName = file.originalname.replace(/\.jpeg|\.jpg|\.png/ig, ''); //RegEx used to remove file expression
        uniqFileName += buf; //These CHARACTERS will be added to the filename
      //Even though the user creates file with same name it will not MATCH bcz of CHARACTERS
      cb(undefined, uniqFileName );
    }
  });

const storage2 = cloudinaryStorage({
    cloudinary,
    allowedFormats: ['mp4', 'webm', 'ogg'],
    params:{
        folder: 'bat1video',
        resource_type: 'video'
    },
    filename: function (req, file, cb) {
        let buf = crypto.randomBytes(16);
        buf = buf.toString('hex'); 
        let uniqFileName = file.originalname.replace(/\.mp4|\.webm|\.ogg/ig, ''); 
        uniqFileName += buf; 
      cb(undefined, uniqFileName );
    }
  });

module.exports = {
    cloudinary,
    storage1,
    storage2
}

在路线上,我正在这样做 我可以这样使用 upload1 upload2 还是每个文件只能上传个上传 对于图像,它可以正常工作,但对于视频,则不能

const { storage1, storage2 } = require('../cloudinary');
const upload1 = multer({ storage:storage1 });
const upload2 = multer({ storage:storage2 });

对于图像,效果很好

router.post('/', isLoggedIn, upload1.array('postImages', 4), asyncErrorHandler(fnc));

但是对于视频而言却不是。 视频存储在云端,在后端访问时,req.files为未定义状态

router.post('/', isLoggedIn, upload1.array('postVideo', 4), asyncErrorHandler(fnc));

0 个答案:

没有答案