如何解决如何在Cloudinary上上传两个具有不同名称的图像而非数组?
我正在使用 NodeJs 和 Mongoose 进行项目。我有一个具有两个不同图像属性(1)customer_profile_image
(2)customer_logo
以及其他客户详细信息的客户架构。我想使用POST API上传两个图像,同时添加一个新客户(非图像数组),并且我希望每个图像的一个链接在Cloudinary中上传后存储在数据库中。我之前曾使用Cloudinary上传一个图像或一组图像,因为上传具有相同名称的图像很容易,但是在这种情况下,我不知道如何上传两个具有不同名称的图像。我该怎么办? Cloudinary的上传功能是否使用多个参数?以及,如果确定,如何确定服务器上当前正在上传哪个图像以及哪个图像的链接?
以下是我用于在Cloudinary上上传单个图像的功能:
app.post('/customer',upload.single('customer_profile_image'),async (request,response,next) => {
const path = request.file.path;
var uniqueFilename = new Date().toISOString();
var img = await cloudinary.uploader.upload(
path,{
public_id: `blog/${uniqueFilename}`,tags: `blog`
},// directory and tags are optional
async function (err,image) {
if (err) return (err)
fs.unlinkSync(path);
// console.log(image);
var customer = new Customer(request.body);
customer.customer_profile_image= await image.secure_url
})
})
解决方法
您需要在自己的上传调用中分别上传每个图像,并指定该图像的公共ID。 Cloudinary不支持在一个上传调用中上传多个图像。此外,如果将上载调用更改为使用folder参数而不是公共ID,它将在末尾附加一个随机字符串。例如,Cloudinary上载全部如下所示:
var customer = new Customer(request.body);
const path = request.file.path;
var customer_profile_image = await cloudinary.uploader.upload(
path,{
folder: `blog/customer_profile_image`,// public id = blog/customer_profile_image/<randomly-generated-string>
// e.g. blog/customer_profile_image/djqhfhwidhalwdi
tags: `blog`
},// directory and tags are optional
async function (err,image) {
if (err) return (err)
customer.customer_profile_image = image.secure_url
})
类似地,您需要为customer_logo进行单独的上传调用
var customer_logo = await cloudinary.uploader.upload(
path,{
folder: `blog/customer_logo`,// public id = blog/customer_logo/<randomly-generated-string>
// e.g. blog/customer_logo/sdjoqnfhjahslq
tags: `blog`
},image) {
if (err) return (err)
customer.customer_logo = image.secure_url
})
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。