Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload JavaScript File object to Cloudinary using node js api?

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?

like image 995
Arjun Singh Avatar asked Jul 31 '26 15:07

Arjun Singh


1 Answers

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);
}
like image 179
Vishal Hasnani Avatar answered Aug 02 '26 09:08

Vishal Hasnani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!