1
votes

I am trying to add a new path to folder I created on cloudinary.

Here is my profilesAPI.js:

router.post("/upload/image", function (req, res, next) {
  const dUri = new Datauri();

  const dataUri = (req) =>
    dUri.format(path.extname(req.name).toString(), req.data);

  if (req.files !== undefined && req.files !== null) {
    const { file, id } = req.files;

    const newFile = dataUri(file).content;

    cloudinary.uploader
      .upload(newFile)
      .then((result) => {
        const imageUrl = result.url;
        const data = { id: req.body.id, imageUrl };
        updateAvatar(data);
        return res.status(200).json({ message: "Success", data: { imageUrl } });
      })
      .catch((err) =>
        res.status(400).json({ message: "Error", data: { err } })
      );
  } else {
    return res.status(400).json({ message: "Error" });
  }
});

So right now my url after upload image to cloudinary look like this :

https://res.cloudinary.com/teammateme/image/upload/v1617941466/zwpbarfxvd94olczjjpa.png

and I want to make the url like this:

https://res.cloudinary.com/teammateme/image/upload/v1617941466/UserAvatar/zwpbarfxvd94olczjjpa.png

1

1 Answers

0
votes

You can add a folder as part of the optional parameters for the upload() method call to indicate in which folder the asset should be stored in.

For example, updating the method call to Cloudinary like so:

cloudinary.uploader
   .upload(
     newFile,
     {
       folder: "UserAvatar"
     }
   )

All other upload parameters can be found in the following section of the documentation: https://cloudinary.com/documentation/image_upload_api_reference#upload_method