Closed ghost closed 9 years ago
nvm it works really well thanks!
@karlpower6 no problem, happy it works for you!
I've actually switched over to cordova-plugin-file-transfer and the Cloudinary HTTP API to get it working on Android as well, I'm happy to give you some sample code if you're interested.
Hey @mablack that would be awesome as I'm already using file transfer plugin.
@karlpower6 no worries!
I'm using the jshashes library to generate the SHA1 hash required for the Cloudinary API, so you'll need to install that or something similar.
I haven't included any code to get the file path to the image, but this code should work with the file:// URLs from either the Cordova Camera plugin, or the cordova-plugin-file plugin.
var Hashes = require('jshashes');
// your Cloudinary keys
var CLOUD_NAME = "xxx",
API_KEY = "xxx",
API_SECRET = "xxx";
var uri = encodeURI('https://api.cloudinary.com/v1_1/'+ CLOUD_NAME +'/image/upload');
// e.g. file:///path/to/file/image.jpg
var fileToUploadPath = "xxx";
var options = new FileUploadOptions();
options.fileKey="file";
options.fileName=fileToUploadPath.substr(fileToUploadPath.lastIndexOf('/')+1);
var timestamp = Math.floor(Date.now() / 1000);
// add in the params required for Cloudinary
options.params = {
api_key: API_KEY,
timestamp: timestamp,
signature: new Hashes.SHA1().hex('timestamp='+timestamp+API_SECRET)
};
var ft = new FileTransfer();
ft.onprogress = (function(progressEvent) {
try {
if (progressEvent.lengthComputable) {
// if we can calculate the length of the upload ...
var percentageComplete = ((progressEvent.loaded / progressEvent.total) *100);
} else {
// otherwise increment some counter by 1
}
// do something with the latest progress...
} catch(e){
// handle errors
}
});
ft.upload(fileToUploadPath, uri,
function(result){
// success!
var response = JSON.parse(result.response);
console.log(response);
console.log('===== response =====');
console.log(response);
/*
response is the JSON returned from Cloudinary on successful upload:
{
bytes = 4299687;
"created_at" = "2015-03-31T05:24:52Z";
etag = 38825bcbea005ba3c5da79591625f098;
format = jpg;
height = 2448;
"public_id" = e9fz4zcrvf5n4clmlh1s;
"resource_type" = image;
"secure_url" = "https://.../e9fz4zcrvf5n4clmlh1s.jpg";
signature = d87e52bd9facd534cf2c6bdc3a6707a97036232c;
tags = (
);
type = upload;
url = "http://.../e9fz4zcrvf5n4clmlh1s.jpg";
version = 1427779492;
width = 3264;
}
*/
},
function(error) {
// fail!
},
options
);
@mablack thank you so much! I'll try this out today :)
hum... the API_SECRET in your client app... a small node server would really help here
Do you unzip the ios sdk to the same ios directory inside the plugin directory? So the cloudinary_sdk folder is next to CDVCloudinaryUpload.h and CDVCloudinaryUpload.m files?