【问题标题】:Download .csv File with angular 2 - Ionic 2使用 angular 2 下载 .csv 文件 - Ionic 2
【发布时间】:2017-09-29 21:28:01
【问题描述】:

我有一个 Ionic 2 应用程序,我需要从服务器下载 sample.csv 文件。我已经有了执行请求所需的 url:

 let url = 'http://example.com/download/sample.csv';

我想知道如何做到这一点。我尝试使用 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 的警报消息,但设备中没有下载任何文件。我搜索了整个手机磁盘,但没有找到文件。

如果我把这个网址放在浏览器中,它会开始下载,但不是通过 FileTransfer。

【问题讨论】:

  • 您是否完全按照这里提到的ionicframework.com/docs/native/transfer 进行操作,即下载插件,检查清单文件中的适当权限以防您在棉花糖版本上使用它并打印文件路径,即this.file.dataDirectory
  • 是的,我已按照所有步骤操作。我在哪里可以找到这个清单文件?是的,我正在使用 Marshmallow。
  • 在 ionic 应用程序中会有 config.xml 文件,您可能需要将其添加到那里,或者您可以使用此处的插件 npmjs.com/package/cordova-plugin-android-permissions
  • 请检查取自工作离子文件传输示例的替代可能答案

标签: angular typescript ionic2


【解决方案1】:

不要使用this.file.dataDirectory + file_name.csv 作为您的文件路径,而是将您的代码修改为:

首先在你的构造函数中添加这个:

为了确保您使用的是正确的文件路径,假设您也可以在 ios 上使用它

  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();
        }
    });
}

请注意我所做更改的文件路径的差异。 这段代码sn-p取自dsgriffin ionic file tranfer repofile

【讨论】:

  • 我现在尝试了这个,但没有运气。返回的存储文件是this.storageDirectory 路径为file:///data/user/0/com.ionicframework.myapp8211/files/
  • 使用下载后检查文件夹,因为这似乎是有效的文件存储位置,只需转到com.ionicframework.myapp8211/的文件夹你应该看到你的文件
  • 检查here 以了解如何按照插件指定的方式在系统中存储文件的详细信息,根据此您应该在应用程序的文件夹中找到您的文件,如果您看不到您的应用程序的文件夹可能想要使用不同的文件管理器,例如 es 文件资源管理器或实体资源管理器
  • @AkashM 你找到你下载的文件了吗?
  • 不,我在开发者浏览器上测试过,但没有运气。当我在 chrome 开发者浏览器上运行它时,下面是我点击测试文件下载时收到的错误消息:XMLHttpRequest cannot load http://example.com/download/sample.csv. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access.
猜你喜欢
  • 2018-03-06
  • 1970-01-01
  • 2017-07-27
  • 2016-06-26
  • 1970-01-01
  • 2018-01-09
  • 1970-01-01
  • 2017-11-10
相关资源
最近更新 更多