Question

I need to append file using HTML5 FileWriterSync, multiple blocks of data coming from server. I was tried to append but it was just writing a block of file only. Let's check my code.

var creator;
try {
    fileEntry = fs.root.getFile(filename, {
        create: creator
    });

    var byteArray = new Uint8Array(data.length);
    for (var i = 0; i < data.length; i++) {
        byteArray[i] = data.charCodeAt(i) & 0xff;
    }

    var BlobBuilderObj = new WebKitBlobBuilder();
    BlobBuilderObj.append(byteArray.buffer);

    if (!creator) fileEntry.createWriter().seek();
    fileEntry.createWriter().write(BlobBuilderObj.getBlob())

} catch (e) {
    errorHandler(e);
}

fs has been initialised before, creator changes for appending file and I need to append file.

My problem is how can I call seek to start writing from the EOF.

Was it helpful?

Solution

seek() takes an integer value for the offset. You need to set it to the file's length to do an append:

var fw = fileEntry.createWriter();
fw.seek(fw.length);

It should be similar to the async case: http://www.html5rocks.com/en/tutorials/file/filesystem/#toc-file-appending

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top