Wasim
Wasim

Reputation: 109

ionic 3 how to download pdf file from amzon s3 url

I want to download pdf file into device/browser from amazon s3 bucket url in Ionic3.

I have already tried below solution but its not working.

Storage.get('test.pdf', { download: true })
    .then(result => {
        console.log(Utf8ArrayToStr(result.Body));
    })
    .catch(err => {
        console.log('error axios');
        console.log(err)
    });

Upvotes: 0

Views: 906

Answers (1)

ram12393
ram12393

Reputation: 1258

Install fileTransfer cordova plugin using the following command

ionic cordova plugin add cordova-plugin-file-transfer
npm install @ionic-native/file-transfer

check official doc

and install File cordova plugin ionic cordova plugin add cordo

ionic cordova plugin add cordova-plugin-file
npm install @ionic-native/file

check official doc

and import it in app.module.ts

import { FileTransfer } from '@ionic-native/file-transfer/ngx';
import { File } from '@ionic-native/file/ngx';

and import those in the providers section

providers: [
    FileTransfer,
    File,
  ]

and then write the following code to download the file

in the constructor

  constructor(
    private transfer: FileTransfer,
    private file$: File) { }

and download method

 download() {
    const url = 'www.amazon.com/sample.pdf' // keep your s3 bucket pdf file url
    const fileName = new Date().toISOString().replace(/[:.]/g, '-'); // mention your file name(here I mention current date and time)
    const fileTransfer: FileTransferObject = this.transfer.create();
    const dir_name = 'Download/your_project/'; //mention the directory where you want to download in the device
    let path = '';
    const result = this.file$.createDir(this.file$.externalRootDirectory, dir_name, true);
    result.then((resp) => {
      path = resp.toURL();
      console.log(path); //it will give you the path name
      fileTransfer.download(url, path + fileName + '.pdf').then((entry) => {
        console.log('download complete: ' + entry.toURL());
      });
    });
  }
}

hope it will solve your problem !!

Upvotes: 1

Related Questions