How to create multiple folders and upload them in specific folders using Nodejs ,multer and express?
I am creating an express app Studentforum like any
social media with my friend . Intially we are using cloudinary
for storing images and we want put profile pictures(storage) and
post's images(storage2) in different folder. While using 'storage'
everything works fine but for 'storage2', image buffer is created
instead of url and in cloudinary no folder named 'StudentForumPosts'
is being created by the code. The following code I am using for
cloudinary 'index.js':
const cloudinary = require('cloudinary').v2;
const {CloudinaryStorage}=require('multer-storage-cloudinary');
cloudinary.config({
cloud_name: process.env.CloudinaryCloudName,
api_key: process.env.CloudinaryKey,
api_secret: process.env.CloudinaryApiSecret
});
const storage = new CloudinaryStorage({
cloudinary,
params: {
folder:'StudentForum',
allowedFormats: ['jpg','png','jpeg'],
transformation: [{
width: 300,
height: 300,
gravity: "faces",
crop: "fill"
}],
}
});
const storage2 = new CloudinaryStorage({
cloudinary,
params: {
folder:'StudentForumPosts',
allowedFormats: ['jpg','png','jpeg']
}
});
module.exports = {
cloudinary,
storage,
storage2
}
Post.js:
const express = require('express');
const router = express.Router();
const Post = require('../models/post')
const multer = require('multer');
const {storage2} = require('../cloudinary')
const upload = multer({storage2});
router.get('/new', (req, res) => {
res.render("newpost");
});
router.post('/new', upload.array('image'), (req, res) => {
console.log(req.body,req.files);
res.redirect('/');
});
module.exports=router;
1
-
Hi Kongka,
Thanks for reaching out.
Can you share your cloud name and any logs you get? Especially if you can share the `console.log(req.body,req.files);` value. If you can also share the frontend code and the storage upload that could also be helpful.
On another note, did you try using `single` instead of `array` for your `storage2`? Do you mind trying and see if that works?
On a side note, the plugin you are using is not developed by Cloudinary.
Thanks in advance.
Best,
Loic
0
Post is closed for comments.
Comments
1 comment