Search code examples
cordovaionic2cordova-pluginshybrid-mobile-appionic3

Ionic File Transfer Plugin not working in production version


I am facing weired issue in ionic3 application.

Let me describe my situation in detail: Actually I need offline support for my ionic app. so everytime I call the API, I store the data into local storage. and also download image from the api to my local directory. so that I can fetch data and image when internet is not available from local resources.

I am using this plugin to download image from server to local: https://ionicframework.com/docs/native/file-transfer/

It is working fine if I run following command:

ionic cordova run android

But it is not working when I run following command:

ionic cordova run android --prod

Code :

import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer';
import { File } from '@ionic-native/file';

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

const fileTransfer: FileTransferObject = this.transfer.create();

download() {
  const url = 'http://www.example.com/file.pdf';
  fileTransfer.download(url, this.file.dataDirectory + 'file.pdf').then((entry) => {
    console.log('download complete: ' + entry.toURL());
  }, (error) => {
    // handle error
  });
}

I am not getting any error or issue from console. So I don't know what i am missing. There is also permission for local storage configured well. so permission is not an issue.


Solution

  • Finally I find solution for this issue! at the first you should update this commands:

    npm i @ionic/app-scripts@latest --save
    npm i ionic-native@latest --save
    

    And probably somewhere in your code you call anything related to file-transfer plugin before

    platform.ready.then()

    In my case: I inject some service that include a line like this :

    this.fileTransfer = this.transfer.create();

    And I changed it to this :

    this.platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      this.fileTransfer = this.transfer.create();
    });
    

    Now everything work fine.

    More Details :

    Why this work in debug mode?

    the answer is very clear, because in debug mode device ready event give a long time for fire and file-transfer called after this absolutely! But in production mode, device ready fired very fast and file-transfer called before this. I hope this help you.