如何使用Node JS API将JavaScript File对象上传到Cloudinary? [英] How to upload JavaScript File object to Cloudinary using node js api?

查看:203
本文介绍了如何使用Node JS API将JavaScript File对象上传到Cloudinary?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将文件上传到图像服务器,然后从我的节点js API中选择 cloudinary
i安装了 cloudinary的npm软件包,并按照其$ b $使用了代码b 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

这是我的功能

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?

推荐答案

回答您的问题


如何在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 of upload. 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆