将图片Blob上传到Cloudinary

将图片Blob上传到Cloudinary

本文介绍了将图片Blob上传到Cloudinary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在前端使用react和react-router,node + express作为我的后端,使用cloudinary存储我的图像文件.

Im using react and react-router on the frontend, node+express as my backend and cloudinary to store my image files.

我遇到的问题是cloudinary api方法似乎无法打开/解析存储图像的数据blob

The issue Im having is that the cloudinary api method cant seems to open/parse the data blob where the image is stored

 { images: { preview: 'blob:http://localhost:8080/19526dcc-b67d-4697-b112-e5480de61d03' } }

    cloudinary.v2.uploader.upload(body.images.preview, function(result) {
       console.log(result)
    })

错误响应:

{ Error: ENOENT: no such file or directory, open 'blob:http://localhost:8080/e7f30c71-7e06-4c36-801f-49666e9df053'
  at Error (native)
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: 'blob:http://localhost:8080/e7f30c71-7e06-4c36-801f-49666e9df053'
}

不确定这是由于react-router引起的问题还是应该将数据转换为其他格式?

Not sure if this is a issue due to react-router or should I convert the data to a different format?

路线如下:

app.use('/api/', posts);
app.use('/api/', users);
app.use(express.static(staticPath));
app.use('/', express.static(staticPath));
app.use('/posts/*', express.static(staticPath));
app.use('/new/*', express.static(staticPath));
app.use('/validateEmail/*', express.static(staticPath));

推荐答案

Cloudinary的upload方法为file参数接受以下类型:

Cloudinary's upload method accepts the following types for the file parameter:

  • 本地文件路径(仅在Cloudinary SDK中受支持)
  • 实际数据(字节数组缓冲区).例如,在某些Cloudinary SDK中,这可能是数据的IO输入流(例如File.open(file,"rb")).
  • 数据URI(Base64编码),最大〜60MB(62,910,000个字符)
  • 现有的公共可访问文件的远程FTP,HTTP或HTTPS URL地址
  • 白名单存储桶的S3 URL

如果您能够访问Blob数据(而不仅仅是Blob URL),则可以将其转换为数据URI,Cloudinary的upload方法的file参数将接受该数据URI.有关将Blob转换为数据URI的帮助,请参见此StackOverflow答案.

If you're able to access the blob data (not just the blob URL), it can be converted to a data URI which will be accepted by the file parameter to Cloudinary's upload method. For help with converting a blob to a data URI, see this StackOverflow answer.

这篇关于将图片Blob上传到Cloudinary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 08:50