Angular 2从URL下载文件

时间:2017-12-10 02:34:16

标签: javascript angular ionic2

您好我已将Angular / Ionic 2+应用程序连接到Amazon S3。在我的应用程序中,当我想下载文件时,我的后端会提供从我的S3生成的唯一预签名URL。看起来像这样:

https://s3.ap-southeast-2.amazonaws.com/test%2F1/435345345-4545-4aa8-8a8a-etertertttrt-4addd157-ac38-41fd-5454-8ffa709ee77fcsdfsdfa ...

我的问题是,如果我有网址,如何通过我的组件以编程方式下载文件?到目前为止,我一直没有运气:

window.open(url, '_blank');

getFile(url: string): void {
    this.http.get(url)
    .map((res) => {
        return new Blob([res.blob()], { type: 'image/jpeg' })
    })
    .subscribe(res => {
      saveAs(res,'test.jpg');
      let fileURL = URL.createObjectURL(res);
      window.open(fileURL);
    }, error => {
        console.log(error);
    });
    console.log('Get File http');
  }

使用上面的代码我得到一个错误:请求主体不是blob或数组缓冲区。

感谢您的任何帮助..

1 个答案:

答案 0 :(得分:0)

查看File Transfer Plugin

这用于下载Ionic 2应用程序中的文件。请注意,最新版本的插件(1.7.0)中存在一个错误,该错误发生在构建应用程序时。我目前正在使用1.6.3版本。因此,请使用以下命令安装插件:

Ionic cordova plugin add cordova-plugin-file-transfer@1.6.3
相关问题