cloudinary -base 64 图片上传给出无效 JSON 响应

cloudinary -base 64 image upload giving invalid JSON response

我正在尝试在 CLOUDINARY 上上传 base64 格式的图片

cloudinary.config({
  cloud_name: CLOUDINARY_NAME,
  api_key: CLOUDINARY_API_KEY,
  api_secret: CLOUDINARY_API_SECRET,
});

我尝试过的两种方法

1.  Let newPhoto='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='//hardcoded value
try {
const myCloud = await cloudinary.v2.uploader.upload(newPhoto, {

folder: "UserPhoto",
width: 150,
crop: "scale",
});

}catch (error) {

return next(new ErrorHandler(error.message, 500));   }

2.  cloudinary.v2.uploader.upload("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==",
function(error, result) {console.log(result, error); });

我收到以下回复

undefined {
  message: 'Server return invalid JSON response. Status Code 404. SyntaxError: Unexpected token < in JSON at position 0',
  name: 'Error',
  http_code: 404
}

错误:服务器 return 无效 JSON 响应。状态代码 404。SyntaxError:位置 0

中 JSON 中的意外标记 <

我使用了他们在官方文档中提供的相同 base64 图像字符串

这是link-https://cloudinary.com/documentation/upload_images#file_source_options
section-Upload via a base64 data URI

如何在cloudinary中上传base64图片

您可以使用 Node.js v2 (https://cloudinary.com/documentation/node_integration) 作为后端服务器实现,如下所示:

var cloudinary = require('cloudinary').v2; 

cloudinary.config({ 
   cloud_name: '<YOUR_CLOUD_NAME>', 
   api_key: '<YOUR_API_KEY>', 
   api_secret: '<YOUR_API_SECRET>',
   secure: true
});

var newPhoto = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";

cloudinary.uploader.upload(newPhoto, 
{ 
    folder: "UserPhoto",
    width: 150,
    crop: "scale",
},
function(error, result) {console.log(result, error); });