本文介绍了如何使用Node JS API将JavaScript File对象上传到Cloudinary?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将文件上传到图像服务器,然后从我的节点js API中选择。
i安装了,并按照其$ b $使用了代码b
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 theirapi documentation
这是我的功能
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);
}
出现错误 file.match不是函数。
Getting error "file.match is not a function".
如何在cloudinary上使用文件对象上传图像?
how to upload image using file object on cloudinary?
推荐答案
回答您的问题
为了将File对象上传到cloudinary,可以使用upload_stream方法而不是
上传。在检查文档。
In order to upload File object to cloudinary, you can use upload_stream method instead ofupload. check documentation here.
更正了您的代码:
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);
}
这篇关于如何使用Node JS API将JavaScript File对象上传到Cloudinary?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!