Monday, 24 September 2018

Download CSV file from assets folder in IONIC 3

I have one demo-file.csv file and it is in the assets/csv folder, so how can I download it from mobile,

here is my HTML & COMPONENT code.

HTML CODE

<button ion-button type="button" block (click)="downloadFile('assets/csv/demo-file.csv', 'demo-file.csv')">Download Demo File</button>

COMPONENT CODE

 public downloadFile(link: any, fileName: any) {
      if (link) {
        let path = null;
        this.showWaitingLoading();
        if (this.platform.is('ios')) {
          path = this.file.documentsDirectory;
        } else {
          path = this.file.dataDirectory;
      }

      const transfer = this.transfer.create();

      transfer.download(link, path + fileName).then(entry => {
        this.dismissWaitingLoading();
        this.openFile(entry.toURL());
      }).catch(() => {
        this.dismissWaitingLoading();
        this.showToastMsg('error', "Something went wrong");
      });
     }
    }
/* ================= OPNE FILE FUNCTION ===========*/

public openFile(path: any) {
   this.fileOpener.open(path, 'application/*')
     .then(() => console.log('File is opened'))
     .catch((e: any) => console.log('Error openening file', e));
}

I'm not able to download the file, is there any thing missing in my PATH?



from Download CSV file from assets folder in IONIC 3

No comments:

Post a Comment