Question

I'm using Phonegap 2.0 and try to get a media encoded as base64 from its URI with no success.

function tryToSend(fileReader) {
    // I don't really what the parameter is
}

function win(file) {
    alert(file.name + ' ' + file.type); // type is undefined here
    var reader = new FileReader();
    reader.onloadend = tryToSend;
    var encoded = reader.readAsDataURL(file); // encoded is undefined here
}

function fail(error) {
    console.log(error);
}

function onResolveSuccessCompleted(fileEntry) {
    fileEntry.file(win, fail);
}

function onResolveFailed(error) {
    console.log(error);
}

window.resolveLocalFileSystemURI(MY_FILE_URI, onResolveSuccessCompleted, onResolveFailed);

At the end, i'm not able to extract base64 encoded data for my file, I need it to send it in a JSON AJAX call.

Is there something wrong in my code? Do you know a way to achieve what I need?

Cheers.

Cyril

Was it helpful?

Solution

Okay, there is no point to capturing the return value of reader.readAsDataURL as it is an async call and does not return anything. Your tryToSend method should be written:

function tryToSend(evt) {
    var encoding = evt.target.result;
    // now encoding has your file as a base64 encoded string.
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top