使用blob url下载angular 7中的excel文件
我正试图在angular 7应用程序中使用mime类型“data:application/vnd.openxmlformats of icedocument.spreadsheetml.sheet,”下载excel文件,但当我试图打开下载的文件时,它会提示我一些错误,如- 以下是我为了下载该文件所做的工作使用blob url下载angular 7中的excel文件,excel,angular,Excel,Angular,我正试图在angular 7应用程序中使用mime类型“data:application/vnd.openxmlformats of icedocument.spreadsheetml.sheet,”下载excel文件,但当我试图打开下载的文件时,它会提示我一些错误,如- 以下是我为了下载该文件所做的工作 const filePath = "https://bviewstorage.blob.core.windows.net/9be306d9-acb1-4f25-a54f-5126e0
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文件的方法。添加
appenchild
,它应该可以工作
document.body.appendChild(downloadLink);
downloadLink.click();
对于IE,请这样做:
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(url, filename);
}
由于您的文件不安全,可以使用上述代码下载。window.open打开一个新的浏览器窗口,该窗口将直接从blob下载文件。扩展名以对上述解决方案进行注释
submitForm():void{
this.myFormPost.nativeElement.submit();
}
下载文件
这个问题有什么不同吗?我已经试过了also@A_A但是,当我试图打开下载的excel文件时,它仍然会给我带来相同的错误有很多解决方案,你尝试过哪些?例如window.location=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“
在Firefox上为我工作谢谢你的回复。如果我按照你说的做了什么,那么下载的文件名称是从blob路径自动获取的,但我希望下载的文件名是自定义的(我需要根据条件提供文件名)。答案很简单,您尝试做的事情是不可能的。我有同样的要求下载xlxs文件表单blob。我已经搜索了N个以下面的链接结尾的解决方案。有一种方法可以做到这一点。您需要后端API的帮助,它可以为您提供blob。后端API应该从blobURL中提取文件,并返回blob内容。API应该是POST,并且内容类型应该是application/x-www-form-urlencoded。感谢您的回复。我已经按照您所说的进行了尝试,但在尝试打开下载的文件时仍然显示相同的错误
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);
}
function downloadFile(filePath) {
var link = document.createElement('a');
link.href = filePath;
link.onclick = function() {
document.body.innerText = "Downloading";
setTimeout(function() {
document.body.innerText = "";
}, 2000);
}
link.download = filePath.substr(filePath.lastIndexOf('/') + 1);
link.click();
}
downloadFile("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");