Phonegap filewriter writes only blob metadata...?

Weird problem with FileWriter on phonegap (2.8.1). Whenever I try to write a file using filewriter, a file gets created, but the only content in the file is a line that says {"type":"application/pdf","size":235577}. The same with any file - the type and size differ, but that's the only content that gets written into the file.

I ran the same code using Chrome version 28) and it works just fine, but uses Blob rather than WebKitBlobBuilder. So I downloaded an old version Chromium (version 20) which uses WebKitBlobBuilder and that works fine as well.

To check if I was doing something weird, I used FileReader to dump the contents of the file before I tried to write it (using readAsDataURL) - the contents look fine.

So...?

Code is as follows:

function convert2blob(bin, type){
    // takes a BINARY or a BLOB input and returns a blob
    var blob;
    if (bin.size? true: false) {    
        console.log("its a blob already"); // do nothing??     Line 3494
        blob = bin;
    } else {
        var arr = new Uint8Array(bin.length);
        for (var i = 0; i < length; i++) {
            arr[i] = bin.charCodeAt(i)  & 0xff;
        }
        var bl = new window.BlobBuilder();
        bl.append(arr.buffer);
        blob = bl.getBlob(type);
    }
    return blob;
}


    blob = convert2blob(resx, attinfo.contenttype);
    console.log(blob.size);                             // Line 3543
    // just to verify the contents of the blob
    // tested with fileReader - contents are valid
    /* // just to check the file contents
    var rdr = new FileReader();
        rdr.onloadend = function(evt){
            console.log(evt.target.result.slice(0,128));
        };
    rdr.readAsDataURL(blob);
    */
    // its a blob - that's what we decided
    global.tmpFilesystem.root.getFile(attinfo.name, { create: true }, function (fileEntry) {
        console.log(fileEntry);
        fileEntry.createWriter(function (fileWriter) {
            console.log(fileWriter);
            fileWriter.onwriteend = function (e) {
                console.log(e.error);                   // Line 3582
                console.log("finished writing");        // Line 3583
                console.log(e.target.length);           // Line 3584
                runpopup(selectorid, fileEntry.toURL());
            };
            fileWriter.onerror = function(e) {
                console.log(e);
            };
            fileWriter.write(blob);
        }, function(e) {
            console.log(e);
        });
    }, function(e) {
        console.log(JSON.stringify(e));
    });

Logcat output looks like:

I/Web Console(19696): {"encoding":null,"name":"005.jpg","fullPath":"005.jpg","contenttype":"image/jpeg"}:3521
I/Web Console(19696): /9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEB:5236
I/Web Console(19696): 516857:5240
I/Web Console(19696): using WebKitBlobBuilder:5246
I/Web Console(19696): 516857:5250
// above few lines are after fetching from Websql DB

I/Web Console(19696): [object Blob]:3541
I/Web Console(19696): its a blob already:3494
I/Web Console(19696): 516857:3543
I/Web Console(19696): [object Object]:3560
I/Web Console(19696): [object Object]:3580
I/Web Console(19696): undefined:3582
I/Web Console(19696): finished writing:3583
I/Web Console(19696): 36:3584

Updated with browser output ============ Comparable output from Chromium 20:

{"encoding":null,"fullPath":"closed-padlock.png","name":"closed-padlock.png","doc_id":"FE261266-B04C-48F3-8557-7FCF160E7F9F","contenttype":"image/png"}  - :3521
iVBORw0KGgoAAAANSUhEUgAAAS4AAAHXCAYAAAAC3beSAAAABmJLR0QA/wD/AP+gvaeTAAAXWElEQVR4nO3debDVdf3H8de9XJZAsACX1CwNwWVSGhfck2LMTEzDpazB  - :5236
6051  - :5240
using WebKitBlobBuilder  - :5246
6051  - :5250
Blob - :3541
its a blob already  - :3494
6051  - :3543
FileEntry - :3560
FileWriter - :3580
undefined  - :3582
finished writing  - :3583
6051  - :3584
filesystem:file:///temporary/closed-padlock.png 

Upvotes: 1

Views: 1476

Answers (1)

So, the code is identical - the only difference is I upgraded from 2.8.1 to 3.0.0 this afternoon (via, 2.7, 2.8 and 2.9 - while trying to isolate the problem) and the FileWriter problem has gone. Files are getting written correctly. Simon's guide was a huge help in getting the plugins converted - I still have one plugin problem remaining, but that's not a huge issue - should be able to get that sorted fairly soon (I think). Turns out it wasn't a plugin problem after all and everything is working great.

BTW - for those who weren't aware, its quite instructive to look through the cordova.js file - discovered that a base64 codec has been incorporated into there - hope its compatible with the CouchDB codec...

Upvotes: 2

Related Questions