繁体   English   中英

将 docx 下载为 blob

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM