Writting a file to the device

Moderator: Support

kevinmcisaac
Posts: 37
Joined: Mon Oct 05, 2020 8:27 am

Writting a file to the device

Post by kevinmcisaac »

I want to write out a file to my device. Do you have an example or instruction on using the Cordova File plugin to write a file to the device.

Based on the tutorial I've already got the File plug in installed and configured and use it to read a file.

When looked at the documentation https://cordova.apache.org/docs/en/late ... orary-file
I first need to create a file using

Code: Select all

window.requestFileSystem(window.TEMPORARY, 5 * 1024 * 1024, function (fs) {
    console.log('file system open: ' + fs.name);
    createFile(fs.root, "newTempFile.txt", false);
}, onErrorLoadFs);

however window is not defined and requestFileSystem is not on the File object from the plugin

Serhii Kulibaba
Posts: 147
Joined: Tue Aug 27, 2013 1:47 pm

Re: Writting a file to the device

Post by Serhii Kulibaba »

Hello,

We don't have any specific instructions for that. All examples from plugin's repository page are acceptable in Appery.io

kevinmcisaac
Posts: 37
Joined: Mon Oct 05, 2020 8:27 am

Re: Writting a file to the device

Post by kevinmcisaac »

Incase anybody else is needed to do this, after a a lot of reading stackoverflow I learnt:

  • Use "globalThis.window" in stead of "window". I guess this is an angular thing

  • Use "webkitRequestFileSystem" instead of "requestFileSystem" in chrome

Here is the full code

Code: Select all

//https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/#writeFile
//https://stackoverflow.com/questions/12748920/simple-example-with-window-requestfilesystem-function/12750638
const _that = this;
function writeFile(fileEntry, str) {
    // Create a FileWriter object for our FileEntry
    fileEntry.createWriter(function (fileWriter) {

    fileWriter.onwriteend = function(res) {
        console.log("writeFile OK: ", res);
        //https://ionicframework.com/docs/native/file-opener
        _that.fileOpener.open(res.target.localURL, 'application/vnd.google-earth.kml+xml')
          .then((res) => console.debug('fileOpener.open OK: ', res))
          .catch(e => console.log('fileOpener.open Error:', e));
    };

    fileWriter.onerror = function (e) {
        console.log("Failed file write: " + e.toString());
    };

    const dataObj = new Blob([str], { type: 'text/plain' });
    fileWriter.write(dataObj);
});
}

function createFile(dirEntry, fileName, str) {
    // Creates a new file or returns the file if it already exists.
    dirEntry.getFile(fileName, {create: true, exclusive: false}, 
        function(fileEntry) {
            console.debug('createFile: ',  fileEntry);
            writeFile(fileEntry, str);
        }, 
        onErrorCreateFile);
}

let onErrorCreateFile = (err) => console.error(err);
let onErrorLoadFs = (err) => console.error(err);

const window:any = globalThis.window; // Find window in Angular??
//https://stackoverflow.com/questions/6803064/window-requestfilesystem-not-working
//TEMPORARY or PERSISTENT
const requestFS = window.requestFileSystem || window.webkitRequestFileSystem;// depends on which platfrom we run on 
requestFS(window.TEMPORARY, 5 * 1024 * 1024, function (fs) {

console.debug('requestFileSystem: ',  fs);
createFile(fs.root, "track.kml", KMLstr);

}, onErrorLoadFs);


console.groupEnd();
Serhii Kulibaba
Posts: 147
Joined: Tue Aug 27, 2013 1:47 pm

Re: Writting a file to the device

Post by Serhii Kulibaba »

Glad to hear things are sorted out now! Thank you for this update!

Post Reply