ionic-frameworkionic3cordova-pluginsionic-nativecordova-plugin-file

Cordova Ionic Native File plugin - Where does it store the files in iOS?


I been trying to save a file on iOS. Right now I been running through Xcode right to my phone.

When I run the app it says that is saved successfully. But I don’t see any file when I use a file manager app like FileApp, FileManager, iCloud and the Apple crappy Files app.

My question is that I heard from a web search that in order to save the file iOS creates a sandbox folder for the app.

If I been saving it to this.file.documentDirectory, how can a user open it in let’s say Pages or Numbers apps? (You know Apple’s Word and Excel replacement for the uninitiated.)

Here’s the code.

 writeToIOS(opts : {
    fileName: string,
    text: any
  }) : Promise < IResponse < any >> {
    let response: IResponse < boolean > = {
      success: false,
      error: '',
      data: false
    };
    const options: IWriteOptions = {
      replace: true
    };
    return new Promise((resolve, reject) => {
      const path = this.file.documentsDirectory;
      const directory = 'Attendance Log';
      this
        .file
        .checkDir(path, directory)
        .then(res => {
          this
            .file
            .writeFile(path + directory, opts.fileName, opts.text, options)
            .then(res => {
              response = {
                ...response,
                success: true,
                error: res,
                data: res
              };
              resolve(response);
            })
            .catch(error => reject(error));
        })
        .catch(() => {
          this
            .file
            .createDir(path, directory, true)
            .then(directory => {
              this
                .file
                .writeFile(path + directory.name, opts.fileName, opts.text, options)
                .then(res => {
                  response = {
                    ...response,
                    success: true,
                    error: res,
                    data: res
                  };
                  resolve(response);
                })
                .catch(error => reject(error));
            })
            .catch(error => reject(error));
        });
    });
  }

EDIT:

What the client/user should do is be able to choose the file and open it in his/her favorite app. That being a File Manager app or a Writer or Spreadsheet app.


Solution

  • This is using the cordova-plugin-fileopener2 in conjuction with ionic native file opener:

    const options = {
      replace: true
    };
    const path = this.file.documentsDirectory;
    const directory = 'Attendance Log';
    this.file
       .checkDir(path, directory)
          .then(res => {
       this.file
          .writeFile(path + directory, opts.fileName, opts.text, options)
             .then(res => {
       this.fileOpener
          .open(`${path}${directory}/${opts.fileName}` , 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
          .then(() => ...)
          .catch(error => ...);
    })
    .catch(error => ...);