gpt4 book ai didi

javascript - 上传到 Cloudinary API - 文件参数无效

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:03:15 25 4
gpt4 key购买 nike

我正在努力将 Cloudinary 与我的 React Native 应用程序集成,但在使用 Cloudinary API 上传时遇到了问题。我正在使用 React Native Image Picker从相机胶卷中选择一个图像并使用它我得到一个源 uri - 下面的示例。

我从 Cloudinary 收到错误响应,我不确定它指的是什么。 “无效的文件参数。确保您的文件参数不包含‘[]’”

当我使用调试器时,我可以通过控制台注销我在请求正文中发送的所有参数。任何建议将不胜感激!

source.uri:/Users/IRL/Library/Developer/CoreSimulator/Devices/817C678B-7028-4C1C-95FF-E6445FDB2474/data/Containers/Data/Application/BF57AD7E-CA2A-460F-8BBD-2DA6846F5136/Documents/A2F21A21-D08C-4D60-B005-67E65A966E62.jpg

async postToCloudinary(source) {
let timestamp = (Date.now() / 1000 | 0).toString();
let api_key = ENV.cloudinary.api;
let api_secret = ENV.cloudinary.api_secret
let cloud = ENV.cloudinary.cloud_name;
let hash_string = 'timestamp=' + timestamp + api_secret
let signature = CryptoJS.SHA1(hash_string).toString();
let upload_url = 'https://api.cloudinary.com/v1_1/' + cloud + '/image/upload'

try {
let response = await fetch(upload_url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
file: {
uri: source.uri,
type: 'image/jpeg'
},
api_key: api_key,
timestamp: timestamp,
signature: signature
})
});

let res = await response.json();
console.log(res);
} catch(error) {
console.log("Error: ", error);
}

更新所以我现在可以使用 base64 编码,但我仍然遇到同样的错误。

var wordArray = CryptoJS.enc.Utf8.parse(source.uri);
var file = CryptoJS.enc.Base64.stringify(wordArray);

try {
let response = await fetch(upload_url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
file: {
uri: file,
type: 'image/jpeg;base64'
},
api_key: api_key,
timestamp: timestamp,
signature: signature
})
});

最佳答案

原来我传入的源数据格式不正确。我能够从我使用的 ImagePicker 插件将其作为已格式化的数据 URI 传递(ImagePicker 示例带有两种捕获源文件的方法,但我使用了错误的方法)。我能够摆脱 CryptoJS 的东西并简单地传入 file: source.uri

关于javascript - 上传到 Cloudinary API - 文件参数无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40052606/

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