gpt4 book ai didi

javascript - 从 React : am including Cloudinary unsigned preset but get "Upload preset must be specified when using unsigned upload" 上传 Cloudinary 图片

转载 作者:行者123 更新时间:2023-11-29 23:18:38 52 4
gpt4 key购买 nike

我正在尝试基于此代码笔示例构建一个简单的 Cloudinary 图像上传:https://codepen.io/team/Cloudinary/pen/QgpyOK --

我已经将它转换为与 fetch 一起使用,但即使我已经转到我的 Cloudinary 设置并创建了一个未签名的上传预设,我将它提供给标题,我仍然遇到错误

POST https://api.cloudinary.com/v1_1/myproject/upload 400(错误请求)

错误信息

使用未签名上传时必须指定上传预设

我使用的代码如下

_callCloudinaryApi(file, method = 'post') {

const config = {
method
}

const cloudName = "myproject" // fictional project name here
const unsignedUploadPreset = "mypreset" // fictional preset name here
const url = `https://api.cloudinary.com/v1_1/${cloudName}/upload`;


const fd = new FormData();
fd.append('upload_preset', unsignedUploadPreset);
fd.append('tags', 'browser_upload'); // Optional - add tag for image admin in Cloudinary
fd.append('file', file);


if (method !== 'get') {
config.headers = {}
config.headers['X-Requested-With'] = 'XMLHttpRequest'
}

return fetch(url, config)
.then(res => res.json())
.then(res => {
console.log(res)
return res;
})
}

任何人都可以帮我确定可能是什么问题吗?非常感谢!

最佳答案

您可以尝试以下操作-

    var data = new FormData();
data.append('upload_preset', '<upload_preset>');
data.append('file', file);
data.append('cloud_name', '<cloud_name>');

const config = {
method: "POST",
body: data
};

var imgurl = "https://api.cloudinary.com/v1_1/<cloud_name>/image/upload";

fetch(imgurl, config)
.then(responseData => {
console.log(JSON.stringify(responseData, null, 4));
})

关于javascript - 从 React : am including Cloudinary unsigned preset but get "Upload preset must be specified when using unsigned upload" 上传 Cloudinary 图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51633061/

52 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com