我正在尝试基于这个代码示例构建一个简单的Cloudinary图像上传:https://codepen.io/team/Cloudinary/pen/QgpyOK --
我已经将其转换为使用fetch,但即使我已经转到我的Cloudinary设置并创建了一个未签名的上传预设,我正在将其提供给标题,我仍然收到一个错误
POST https://api.cloudinary.com/v1_1/myproject/upload 400 (Bad Request)
并显示错误消息
Upload preset must be specified when using unsigned upload
我使用的代码如下
_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;
})
}有人能帮我确定问题出在哪里吗?非常感谢!
发布于 2018-08-01 20:52:45
你可以尝试下面这样的方法:
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));
})发布于 2018-11-09 18:57:27
我也遇到了这个错误,转到https://cloudinary.com/console/settings/upload,你确实有一个Upload presets,请确保有一个Signing Mode变成了Unsigned。
从安全性的角度来看,这可能不是最好的做法(我想更好的方法是通过身份验证)。
我在Devtools网络选项卡btw中找到了关于400错误的线索。
发布于 2020-01-05 20:58:44
我也遇到了同样的问题,希望这个解决方案能对其他人有所帮助
uploadFile = async (e) => {
const files = e.target.files;
const data = new FormData();
data.append('file', files[0]);
data.append('upload_preset', 'PRESET_NAME');
const res = await fetch('https://api.cloudinary.com/v1_1/{YOUR_ACOUNT_USER}/image/upload', {
method: 'POST',
body: data
});
const file = await res.json();
console.log(file);
this.setState({
image: file.secure_url
})
}https://stackoverflow.com/questions/51633061
复制相似问题