最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - PhoneGap readAsDataURL - Stack Overflow

programmeradmin6浏览0评论

I am writing my first Android app using PhoneGap, but I'm a little confused by the documentation for the 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 pretty much all of that except for the last line: entry.file(win, fail). Nowhere is entry defined, but I assume it is a FileEntry object. The problem is that I have not had much luck finding documentation on how to generate the FileEntry object, and at what point I pass in a file path.

I am writing my first Android app using PhoneGap, but I'm a little confused by the documentation for the 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 pretty much all of that except for the last line: entry.file(win, fail). Nowhere is entry defined, but I assume it is a FileEntry object. The problem is that I have not had much luck finding documentation on how to generate the FileEntry object, and at what point I pass in a file path.

Share Improve this question asked Jun 6, 2013 at 14:06 tjc59tjc59 6338 silver badges24 bronze badges 1
  • yes, the documentation is still horrible – El Dude Commented Feb 28, 2016 at 3:09
Add a ment  | 

1 Answer 1

Reset to default 19

Ok, finally got this to work. Horrible documentation online! I'm posting my code in case others are having trouble:

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 () { }
);
发布评论

评论列表(0)

  1. 暂无评论