Angular下载文件

  public Down(path: string) {

    return this.http.get(path, { responseType: "blob" }).subscribe((results) => {

      this.saveExcel(results, "导出数据");
    });

  }
  /**
  * 请求成功之后保存成excel文件(基本导出方法,请求成功之后调用)
  * @param data
  * @param name
  */
  public saveExcel(data: Blob, name: string) {
    var a = document.createElement('a');
    var blob = new Blob([data], { 'type': "application/vnd.ms-excel" });
    a.href = URL.createObjectURL(blob);
    a.download = name + ".csv";
    a.click();
  }

 

posted @ 2018-07-16 17:58  一颗土豆  阅读(1311)  评论(0编辑  收藏  举报