3

I need to upload a file to image server and i choose to go with cloudinary from my node js api. i installed the npm package for cloudinary and used the code as per their api documentation

here is my function which is making a http request call to upload image.

var cloudinary = require('cloudinary').v2;

function uploadProfilePic(req, res, next) {
   let file = (req && req.files.file) ? req.files.file : ''; // File object
   cloudinary.uploader.upload(file, function (error, result) {
      if (!error && result.url) {
         req.body.imageURL = result.url;
         next();
      }
      else {
         req.body.imageURL = '';
         next();
      }
   }).end(file.data);
}

Getting error "file.match is not a function".

how to upload image using file object on cloudinary?

1 Answer 1

4

Answering your question

How to upload image using file object on cloudinary?

In order to upload File object to cloudinary, you can use upload_stream method instead of upload. check documentation here.

Corrected your code:

var cloudinary = require('cloudinary').v2;
function uploadProfilePic(req, res, next) {
   let file = (req && req.files.file) ? req.files.file : '';
   cloudinary.uploader.upload_stream({ resource_type: 'raw' }, function (error, result) {
      if (!error && result.url) {
         req.body.imageURL = result.url;
         next();
      }
      else {
         req.body.imageURL = '';
         next();
      }
   }).end(file.data);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.