如何使用angular从服务器下载文件?

时间:2018-12-19 13:53:24

标签: angular http

    this.repositoryApiService.export(repoType, repoItem, type)
      .subscribe((data: any) => {
        this.commonService.downloadFileFromServer(data.serverPath, data.name);
      }, error => {
        console.error(error);
      });
  }

 downloadFileFromServer(serverUrl: string, fileName: string) {
        const a = document.createElement("a");
        document.body.appendChild(a);
        a.setAttribute("style", "display: none");
        a.href = serverUrl;
        a.download = fileName;
        a.click();
        a.remove(); // remove the element
    }

我从服务器获取的数据包含url

  • doc文件已下载,但pdf文件已在浏览器本身中打开

0 个答案:

没有答案