[英]Uploading to Cloudinary API - Invalid file parameter
I am working on integrating Cloudinary with my React Native app and am running into a problem when I go to upload using the Cloudinary API.我正在将 Cloudinary 与我的 React Native 应用程序集成,但在使用 Cloudinary API 上传时遇到了问题。 I'm using React Native Image Picker to select an image from the camera roll and using that I get a source uri - example below.
我正在使用React Native Image Picker从相机胶卷中选择一个图像,并使用它获得一个源 uri - 下面的示例。
I am getting an error response back from Cloudinary and I'm not sure what it's referring to.我从 Cloudinary 收到错误响应,我不确定它指的是什么。
"Invalid file parameter. Make sure your file parameter does not include '[]'"
When I use the debugger, I can console log out all the params I am sending in the body of my request.当我使用调试器时,我可以控制台注销我在请求正文中发送的所有参数。 Any suggestions would be much appreciated!
我们欢迎所有的建议!
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);
}
} }
UPDATE So I now have base64 encoding working, I think, but I am still getting the same 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
})
});
So it turns out the source data I was passing in was not formatted correctly.所以事实证明我传入的源数据格式不正确。 I was able to pass it in from the ImagePicker plugin I was using as an already formatted data URI (the ImagePicker example comes with two ways to capture your source file and I was using the wrong one).
我能够从我使用的 ImagePicker 插件中将它作为已经格式化的数据 URI 传入(ImagePicker 示例提供了两种捕获源文件的方法,但我使用了错误的方法)。 I was able to get rid of the CryptoJS stuff and simply pass in
file: source.uri
我能够摆脱 CryptoJS 的东西,只需传入
file: source.uri
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.