Ionic-native: Write File Support

Created on 1 Jul 2016  路  3Comments  路  Source: ionic-team/ionic-native

In the File plugin, we should add a method for writing files.

It is kind of painful the way it is now. It was very easy in ngCordova. writeFile(path, file, data, replace)

I wrote this method to do it in a small sample app. It works but we need additional rigor around it.

writeFileInternal(directoryPath, fileName, fileContent){
    return new Promise( (resolve, reject) => {
      (<any>window).resolveLocalFileSystemURL(directoryPath, (fileSystem) => {
        fileSystem.getFile(fileName, {create: true}, (fileEntry) => {
          fileEntry.createWriter( (writer) => {
            writer.onwriteend = function (evt) {
              if (this.error) {
                reject(new Error("Failed to write file"));
              } else {
                resolve(evt);
              }
            };

            writer.write(fileContent);

          }, (err) => {
            console.log("ERROR: ", err);
            reject(err);
          });
        });
      });
    });

Thanks,
Dan

Most helpful comment

Hey @danbucholtz

We have a new wrapper for the File plugin that is here in the dev branch: https://github.com/driftyco/ionic-native/blob/dev/src/plugins/file.ts

I should probably have time next week to finalize the plugin and merge it into master.

All 3 comments

Hey @danbucholtz

We have a new wrapper for the File plugin that is here in the dev branch: https://github.com/driftyco/ionic-native/blob/dev/src/plugins/file.ts

I should probably have time next week to finalize the plugin and merge it into master.

You rock @ihadeed !

Any updates about it? :)

Was this page helpful?
0 / 5 - 0 ratings