Search code examples
javascriptangularionic-frameworkionic4capacitor

Unable to download file using ionic file transfer plugin in ionic 5+


here im using ionic file transfer plugin for downloading a file and when ever i tried to download the file im getting below error

401

and also every time i build a file i need to add the below code in android also

public class FileProvider extends androidx.core.content.FileProvider

is there any other alternative for this download . i checked capacitor site but im not sure how to use file storage plugin in that and below is my code using file transfer

 pdfUrl = 'https://www.cs.toronto.edu/~hinton/absps/NatureDeepReview.pdf';


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

  download() {
    const fileTransfer: FileTransferObject = this.transfer.create();
    const url = this.pdfUrl;
  fileTransfer.download(url, this.file.dataDirectory).then((entry) => {
    console.log('download complete: ' + entry.toURL());
    this.saveData = entry.toURL();

  }, (error) => {
    // handle error
    console.log(error);
  });

Solution

  • On Android, cordova-plugin-file-transfer requires cordova-plugin-whitelist.

    Install it with npm install cordova-plugin-whitelist and then run npx cap update

    Also, cordova-plugin-file-transfer requires the path to the file where you want to write, you passed a directory, you should change fileTransfer.download(url, this.file.dataDirectory) to fileTransfer.download(url, this.file.dataDirectory + '/NatureDeepReview.pdf')