如何使用angular 5使用blob函数下载文件

时间:2018-09-05 10:24:59

标签: angular typescript

下载具有Blob功能的任何文件类型

private saveAsBlob(data: any) {
 const blob = new Blob([data._body],
 const file = new File([blob], 'image.png',
 FileSaver.saveAs(file);
}

3 个答案:

答案 0 :(得分:3)

尝试愚弄

saveAsBlob(data: Response){
  var blob = new Blob([data._body], { type: 'image/png' });
  var url= window.URL.createObjectURL(blob);
  window.open(url);
}

答案 1 :(得分:1)

我知道它可以与url一起使用:

download(row) {
    return this.Http
      .get(url, {
        responseType: ResponseContentType.Blob,
      })
      .map(res => {
        return {
          filename: row.name,
          data: res.blob()
        };
      })
      .subscribe(res => {
        let url = window.URL.createObjectURL(res.data);
        let a = document.createElement('a');
        document.body.appendChild(a);
        a.setAttribute('style', 'display: none');
        a.href = url;
        a.download = res.filename;
        a.click();
        window.URL.revokeObjectURL(url);
        a.remove();
      });
  }

答案 2 :(得分:0)

我开始下载的解决方案。我在 NgRX Effect 中使用了它。

// ... getting blob form somewhere
const anchor = document.createElement('a');
anchor.download = "some_file_name.txt";
anchor.href = (window.webkitURL || window.URL).createObjectURL(blob);
anchor.click();
相关问题