Phonegap readAsDataURL

I am writing my first Android application using PhoneGap, but I'm a bit confused by the documentation for FileReader. I need to take an image file and convert it to a Base64 string using the readAsDataURL () method. From their documentation:

function win(file) { var reader = new FileReader(); reader.onloadend = function(evt) { console.log("read success"); console.log(evt.target.result); }; reader.readAsDataURL(file); }; var fail = function(evt) { console.log(error.code); }; entry.file(win, fail); 

I understand almost all of this, except for the last line: entry.file (win, fail). No entry is specified anywhere, but I assume it is a FileEntry object. The problem is that I was not very lucky to find the documentation on how to generate the FileEntry object, and at what point I am passing the file path.

+6
source share
1 answer

Well, finally it worked. Awful documentation online! I am sending my code in case others have problems:

 window.resolveLocalFileSystemURI(filePath, // success callback; generates the FileEntry object needed to convert to Base64 string function (fileEntry) { // convert to Base64 string function win(file) { var reader = new FileReader(); reader.onloadend = function (evt) { var obj = evt.target.result; // this is your Base64 string }; reader.readAsDataURL(file); }; var fail = function (evt) { }; fileEntry.file(win, fail); }, // error callback function () { } ); 
+18
source

Source: https://habr.com/ru/post/946692/


All Articles