THE SITUATION:
In my Ionic 2 app I have a Documents section where the user should be able to download files.
I am trying to setup the file transfer for Ionic 2.
THE CODE:
The documents component:
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Transfer } from 'ionic-native';
declare var cordova: any;
@Component({
selector: 'page-documents',
templateUrl: 'documents.html'
})
export class DocumentsPage {
constructor(public navCtrl: NavController) {}
ionViewDidLoad() {
}
downloadFile()
{
const fileTransfer = new Transfer();
let url = 'http://MY_URL.com/example.txt';
fileTransfer.download(url, cordova.file.dataDirectory + 'example.txt').then((entry) =>
{
console.log('download complete: ' + entry.toURL());
}, (error) => {
// handle error
});
}
}
THE RESULTS:
In the browser:
I get the following error message: FileTransfer is not defined
but probably because cordova doesn't work in the browser.
In the emulator:
In the emulator the download seems actually successfull. In the console I can see: donwload complete
. but I didn't see starting any download and there is no trace of the downloaded file.
In the device:
In the device - after clicking for the download - nothing happens.. I don't see starting any download. In the file manager I don't see the file.
THE QUESTION:
How can I properly setup the file download for Ionic 2?
There is something wrong in the code?
Should I see the download starting in the device?
Thanks!