[英]How to download any file types(dgn,docx etc) using blob in Angular 7
[英]Download docx as blob
我從后端用快遞發送了一個 docx:
module.exports = (req, res) => {
res.status(200).sendFile(__dirname+"/output.docx")
}
我調用它並將其下載為 angular 的 blob:
$http({
url: '/api_cv/cv/gen',
method: "PUT",
responseType: 'blob'
}).success(function (data, status, headers, config) {
var blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' });
var fileName = headers('content-disposition');
saveAs(blob, 'file.docx');
}).error(function (data, status, headers, config) {
console.log('Unable to download the file')
});
它適用於 Chrome 和 Firefox。 在 safari 中會打開一個新選項卡,但不會下載任何文件。 在 IE 中(因為我有一台 mac,所以通過 Azure RemoteApp 進行了測試),我得到“您當前的安全設置不允許下載此文件”。
SaveAs is from https://github.com/eligrey/FileSaver.js/
是否有另一種適用於所有現代瀏覽器和 IE10+ 的瘦身方法?
您可以嘗試以下saveAs
,而不是使用saveAs
:
var url = window.URL || window.webkitURL;
var downloadUrl = url.createObjectURL(blob);
var a = doc.createElement("a");
a.style.display = "none";
if (typeof a.download === "undefined") {
window.location = downloadUrl;
} else {
a.href = downloadUrl;
a.download = fileName;
doc.body.appendChild(a);
a.click();
}
然后當你完成它時移除錨點。 只是想知道是否是保存部分的問題,我對 FileSaver 沒有太多經驗,但這是我過去所做的
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.