Reputation: 21
I’m trying to create an Ionic 3 App that downloads an image from internet and stores it in the device file system. On an Android device all is working fine, on iOS the call to FileTransferObject.download(…) seems to hang doing nothing; I get no error callback and nothing is downloaded. I created a sample repo you can check out at: https://github.com/andreabarani/TestFileTransferPlugin You will find 2 branches in it:
Both versions produce same results on iOS while 1.7.0 makes Android build fail.
Any help is very appreciated, tnx in advance Andrea
Upvotes: 0
Views: 854
Reputation: 21
I found the solution: my mistake was creating the FileTransferObject too soon, probably when platform wasn't yet ready; my code was like this:
@Component({
selector: 'page-home',
templateUrl: 'home.html',
providers: [FileTransfer, File]
})
export class HomePage {
private fileTransfer: FileTransferObject = this.transfer.create(); // <-- here is too soon to create it
...
This is instead the right way of doing it:
@Component({
selector: 'page-home',
templateUrl: 'home.html',
providers: [FileTransfer, File]
})
export class HomePage {
private fileTransfer: FileTransferObject;
...
...
constructor(public navCtrl: NavController, private transfer: FileTransfer, private file: File, private alertCtrl: AlertController, private platform: Platform) {
}
downloadFile() {
// using a local constant is also a valid alternative,
// the important thing is to instantiate the object at this time
// const fileTransfer: FileTransferObject = this.transfer.create();
this.fileTransfer = this.transfer.create();
Regarding Android compilation problems with version 1.7.0 of the FileTransfer plugin, all was solved by updating Android Studio to version 3.0.1 from 2.3.3.
The complete working app is available here: https://github.com/andreabarani/TestFileTransferPlugin
Upvotes: 2