【问题标题】:How to upload JavaScript File object to Cloudinary using node js api?如何使用节点 js api 将 JavaScript 文件对象上传到 Cloudinary?
【发布时间】:2019-11-11 09:27:01
【问题描述】:

我需要将文件上传到图像服务器,我选择从我的节点 js api 使用cloudinary。 我安装了npm package for cloudinary 并按照他们的使用代码 api documentation

这是我的函数,它正在发出 http 请求调用以上传图像。

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 不是函数”。

如何使用 cloudinary 上的文件对象上传图片?

【问题讨论】:

    标签: javascript node.js file cloudinary


    【解决方案1】:

    回答你的问题

    如何使用 cloudinary 上的文件对象上传图片?

    为了将 File 对象上传到 cloudinary,您可以使用 upload_stream 方法而不是 上传。检查文档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);
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-09
      • 2021-05-10
      • 1970-01-01
      • 2021-06-25
      • 1970-01-01
      • 1970-01-01
      • 2016-12-15
      • 2019-04-24
      • 2021-06-05
      相关资源
      最近更新 更多