下载角度为2的.csv文件 - 离子2 [英] Download .csv File with angular 2 - Ionic 2
问题描述
我有一个Ionic 2应用程序,我需要从服务器下载sample.csv文件。我已经有了我需要提供请求的网址:
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';
我想知道如何做到这一点。我尝试使用ionic2 FileTransfer:
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>
我已生成android-debug.apk文件并将其安装在三星手机中。我收到提示消息 Lead Downloaded Successfully
但没有文件在设备中下载。我搜索了整个手机磁盘,但没有找到文件。
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.
如果我把这个网址放在浏览器中,它会开始下载,但不能用FileTransfer。
If I put this url in browser, it starts to download, but not with FileTransfer.
推荐答案
而不是使用 this.file.dataDirectory + file_name.csv
为您的文件路径修改代码as:
Instead of using this.file.dataDirectory + file_name.csv
for your file path modify your code as:
首先在你的构造函数中添加它:
First add this in your constructor function:
以确保你使用的是正确使用的假设你可以在ios上使用它的平台文件路径
to make sure you are using you are using correct file path as per platform assuming you might use it on ios as well
storageDirectory: string = '';
// make sure this is on a device, not an emulation (e.g. chrome tools device mode)
if(!this.platform.is('cordova')) {
return false;
}
if (this.platform.is('ios')) {
this.storageDirectory = cordova.file.documentsDirectory;
}
else if(this.platform.is('android')) {
this.storageDirectory = cordova.file.dataDirectory;
}
else {
// exit otherwise, but you could add further types here e.g. Windows
return false;
}
现在你的代码:
importleads()
{
const fileTransfer: TransferObject = this.transfer.create();
let url = 'http://example.com/download/sample.csv';
fileTransfer.download(url, this.storageDirectory + '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();
}
});
}
请注意我做出更改的文件路径的差异。
此代码段摘自 dsgriffin ionic file tranfer repo 来自 file
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
这篇关于下载角度为2的.csv文件 - 离子2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!