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?
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With