下载 docx 作为 blob

Joe*_*Joe 5 javascript express angularjs

我用快递从后端发送 docx:

module.exports = (req, res) => {
  res.status(200).sendFile(__dirname+"/output.docx")
}
Run Code Online (Sandbox Code Playgroud)

我将其称为 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')
});
Run Code Online (Sandbox Code Playgroud)

它适用于 Chrome 和 Firefox。在 safari 中会打开一个新选项卡,但不会下载任何文件。在 IE 中(通过 Azure RemoteApp 进行测试,因为我有一台 Mac),我得到“您当前的安全设置不允许下载此文件”。

SaveAs is from https://github.com/eligrey/FileSaver.js/
Run Code Online (Sandbox Code Playgroud)

是否有一种适用于所有现代浏览器和 IE10+ 的替代方法?

Dan*_*rds 2

除了使用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();
}
Run Code Online (Sandbox Code Playgroud)

然后在完成后移除锚点。只是想知道是否是保存部分的问题,我对 FileSaver 没有太多经验,但这就是我过去所做的