首页 > 解决方案 > 使用 blob url 以 Angular 7 下载 excel 文件

问题描述

我正在尝试使用 mime 类型“ data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet ”在我的 angular 7 应用程序中下载 excel 文件,但是当我尝试打开下载的文件时,它会抛出一些错误说 - http://prntscr.com/ur1nc4

这是我为下载文件所做的

const filePath = "https://bviewstorage.blob.core.windows.net/9be306d9-acb1-4f25-a54f-5126e021ec6d/hrm/aabe5bd4-940a-4246-979c-581fdaa45808/Client_export_1592830934796-94f5d780-4007-4ae6-b802-53f4fe5509f2.xlsx"
const linkSource = "data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet," + filePath;
        const downloadLink = document.createElement("a");
        downloadLink.href = linkSource;
        downloadLink.download = "ExcelTemplate" + this.datePipe.transform(new Date(), "yyyy-MM-dd") + ".xlsx";
        downloadLink.click();

我什至尝试下载文件路径“xls”MIME 类型,但仍然抛出相同的错误。

请告诉我下载excel文件的方法。

标签: excelangular

解决方案


const filePath = "https://bviewstorage.blob.core.windows.net/9be306d9-acb1-4f25-a54f-5126e021ec6d/hrm/aabe5bd4-940a-4246-979c-581fdaa45808/Client_export_1592830934796-94f5d780-4007-4ae6-b802-53f4fe5509f2.xlsx"

if(window) {
  window.open(filePath);
}

由于您的文件不安全,因此可以使用上述代码下载。window.open 打开一个新的浏览器窗口,它将直接从 blob 下载文件。


推荐阅读