Unable to load base64 image using cordova-plugin-file-transfer
I'm trying to upload my base64 image to my server using cordova-plugin-file-transfer and it still doesn't work. My code looks like this:
photoBase64 = photoBase64.replace('data:image/png;base64,', '');
var url = "http://MYURL.com/path";
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = "photoName.png";
options.mimeType = "image/png";
var ft = new FileTransfer();
ft.upload(photoBase64,
encodeURI(url),
function(result) {
console.log("Code = " + result.responseCode);
console.log("Response = " + result.response);
console.log("Sent = " + result.bytesSent);
resolve("OK");
},
function(error) {
alert("An error has occurred: Code = " + error.code);
console.error("ERROR", error);
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
reject("ERROR");
},
options);
And I am getting the following error with this code:
How can I load a base64 image using the cordova-plugin-file transfer?
Thanks in advance!
source to share
I'm a year late to the party, but I just came to an answer via trial and error:
You should leave "data: image / png; base64" on the line. I am assuming that without this format it is not a valid url.
specifically in your case, remove this line:
photoBase64 = photoBase64.replace('data:image/png;base64,', '');
It was very easy for me to get the download to work.
source to share
The file transfer plugin does not take Base64 strings. You have to use the location of the file (file :: // android / etc). More details about the files plugin to get the file (this plugin is automatically installed by the filetransfer plugin):
https://github.com/apache/cordova-plugin-file
If you really want to use the base64 string you will have to use $ http.post and write the api on the receiving end to recreate the file
source to share