I have an Ionic 2 app, and I need to download sample.csv file from server. I already have the url that I need to do the request:
let url = 'http://example.com/download/sample.csv';
I want to know how to do this. I tried with ionic2 FileTransfer:
importleads(){
const fileTransfer: TransferObject = this.transfer.create();
let url = 'http://example.com/download/sample.csv';
fileTransfer.download(url, this.file.dataDirectory + 'Import_Leads_Sample.csv').then((entry) => {
if(entry) {
console.log('download complete: ' + entry.toURL());
let alert = this.alertCtrl.create({
title: 'Lead Downloaded Successfully',
buttons: [{
text: 'Ok',
}]
});
alert.present();
}
else{
let alert = this.alertCtrl.create({
title: 'No File to download',
buttons: [{
text: 'Ok',
}]
});
alert.present();
}
});
}
<button ion-button (click)="importleads()">Test File Download</button>
I have generated the android-debug.apk file and installed it in Samsung phone. I am getting alert message as Lead Downloaded Successfully
but no file gets downloaded in the device. I searched entire phone disk but no file found.
If I put this url in browser, it starts to download, but not with FileTransfer.
Instead of using
this.file.dataDirectory + file_name.csv
for your file path modify your code as:First add this in your constructor function:
to make sure you are using you are using correct file path as per platform assuming you might use it on ios as well
Now in you code:
Notice the difference in your file path where I have made a change. This code snippet is taken from dsgriffin ionic file tranfer repo from file