Ionic File Transfer Plugin does not work in production version

I am facing a firmware problem in ionic3 application.

Let me describe my situation in detail: Actually, I need offline support for my ionic application. so every time I call the API, I store the data in local storage. as well as upload the image from the api to my local directory. so that I can receive data and images when the Internet is not available from local resources.

I use this plugin to upload an image from a server to a local one: https://ionicframework.com/docs/native/file-transfer/

It works fine if I run the following command:

ionic cordova run android 

But it does not work when I run the 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 errors or problems from the console. Therefore, I do not know what is missing. There is also permission for local storage. therefore resolution is not a problem.

Thanks in extended for your time and reply.

+5
source share
1 answer

Finally, I find a solution to this problem! during the first update, run the following commands:

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

And probably somewhere in your code you are calling something related to the file transfer plugin before

platform.ready.then()

In my case: I am adding some service that includes 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 is working fine.

More details:

Why does this work in debug mode?

The answer is very clear, because in debug mode, the device is ready for an event, it gives a long time for a fire and transfer of files caused after that absolutely! But in production mode, the device is ready to launch very quickly, and file transfer is called before. Hope this helps you.

+6
source

Source: https://habr.com/ru/post/1271102/


All Articles