簡體   English   中英

Angular 2從URL下載文件

[英]Angular 2 Download File from URL

嗨,我已經將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也不是數組緩沖區。

感謝任何幫助。

看一下File Transfer Plugin

這用於在Ionic 2應用程序中下載文件。 請注意,目前該插件的最新版本(1.7.0)中存在一個錯誤,該錯誤會在您構建應用程序時出現。 我目前正在使用1.6.3版。 因此,使用以下命令安裝插件:

Ionic cordova plugin add cordova-plugin-file-transfer@1.6.3

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM