Ionic 2 File Plugin usage examples

大憨熊 提交于 2019-11-27 13:36:05

Ionic 2 comes with a Cordova file plugin wrapper: http://ionicframework.com/docs/v2/native/file/.

The necessary file system paths (e.g. cordova.file.applicationDirectory) you can find here at the documentation of the original plugin: https://github.com/apache/cordova-plugin-file#where-to-store-files. Note that not all platforms support the same storage paths.

I even managed to build a file browser with it. Use it like so:

import {Component} from '@angular/core';
import {File} from 'ionic-native';

...

File.listDir(cordova.file.applicationDirectory, 'mySubFolder/mySubSubFolder').then(
  (files) => {
    // do something
  }
).catch(
  (err) => {
    // do something
  }
);

Here is an example using IonicNative for an app I am working on where I want to send an email with a csv file attachment.

import {EmailComposer} from '@ionic-native/email-composer';
import {File} from '@ionic-native/file';

class MyComponent {
 constructor(private emailComposer: EmailComposer, private file: File) {

 }
 testEmail() {
 this.file.writeFile(this.file.dataDirectory, 'test.csv', 'hello,world,', {replace: true})
     .then(() => {      
       let email = {
         to: 'email@email',
         attachments: [
           this.file.dataDirectory + 'test.csv'
         ],

         subject: 'subject',
         body: 'body text...',
         isHtml: true
       };
       this.emailComposer.open(email);

     })
     .catch((err) => {
       console.error(err);
     });

 }
}

This was tested with ionic 3.7.0 on IOS.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!