I am trying to save an attachment(an Image) to the device which I retrieve from salesforce using the rest api.
Salesforce returns a binary object which I am trying to save to the android filesystem.
I am using ng-cordova file to create the file and write it to the file system.
I am able to create the file and write in to it, but the written file is corrupt, is there something I need to different before I write the file?
Let me know what you think.
I've attached the code below.
$.ajax({ // Get the Attachment from saleforce
url: oauthService.instanceUrl() + data[0].Body,
type: 'GET',
headers: {
'Authorization': 'Bearer' + ' ' + oauthService.accessToken(),
'Content-type': 'image/png'
}
}).then(function ok(binaryImage) {
$cordovaFile.createDir('ExampleDirectory', false).then(function(result) {
// Directory succesfully Created!
var replacedString = result.nativeURL;
replacedString = replacedString.replace('file://', '');
$cordovaFile.createFile(replacedString + 'test.png', true).then(function(result) {
// File succesfully created
$cordovaFile.writeFile(replacedString + 'test.png', binaryImage, {
'append': false
}).then(function(result) {
// Success! image contents succesfully written to file but it does not open succesfully
alert('file write success : ' + JSON.stringify(result));
}, function(err) {
// An error occured. Show a message to the user
alert('file write error : ' + JSON.stringify(err));
});
}, function(err) {
// An error occured. Show a message to the user
alert('file create error : ' + JSON.stringify(err));
});
}, function(err) {
// An error occured. Show a message to the user
});
}, function err(binaryImage) {
alert('NOT OK ' + JSON.stringify(binaryImage));
});
},
function() {
//
});