Recently, I encountered an issue with downloading base-64 images using the Cordova file transfer protocol. When I tried to provide a remote server path like "", the download worked perfectly. However, when I attempted to use a base-64 image path for the filepath, it failed to download.
----Here is a snippet of my code----
function download(){
var imageData = image.src;
imageData = imageData.replace('data:image/png;base64,', '');
var d = new Date();
var imageName = "sample" + d.getTime() + ".png";
//var filepath = encodeURI("https://upload.wikimedia.org/wikipedia/commons/4/4a/Logo_2013_Google.png");
var filepath = encodeURI(imageData);
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
fileSystem.root.getFile(imageName, { create: true, exclusive: true }, function (fileEntry) {
// get the full path to the newly created file on the device
var localPath = fileEntry.fullPath;
// massage the path for android devices (not tested)
if (device.platform === "Android" && localPath.indexOf("file://") === 0) {
localPath = localPath.substring(7);
}
// download the remote file and save it
var remoteFile = filepath;
var fileTransfer = new FileTransfer();
fileTransfer.download(remoteFile, localPath, function (newFileEntry) {
// successful download, continue to the next image
console.log('successful download');
},
function (error) { // error callback for #download
console.log('Error with #download method.', error);
});
});
})
})
}
}
If anyone has insight or expertise in base-64 conversion, your help would be greatly appreciated. Thank you in advance!