Tfe

Ongi etorri tfe-ren webgunera...

2015-07-22-ean

Override a file in FirefoxOS/DeviceStorage

Apaprently, there is no way to directly override a file in DeviceStorage API.

So, the easiest way to override a file is to remove it, then created a new one:


/* Parameters: blob content, and file destination(string) */
function override_file(blob,file)
{
 return new Promise(function(ok, reject)
    {
 var sdcard = navigator.getDeviceStorage('sdcard');

        // Check if existing file exists
        request = sdcard.delete(file);

        // file existed
        request.onsuccess = function() {
            var request = sdcard.addNamed(blob, file);
            request.onsuccess = ok;
            request.onerror = reject
        };

        // file was not existing, but creating a new one anyway
        request.onerror = function() {
            console.log('creating new file! file');
            var request = sdcard.addNamed(blob, file);
            request.onsuccess = ok;
            request.onerror = reject;
        };
   });
}

Tags: JAVASCRIPT