javascript - Download docx as blob - Stack Overflow

I send a docx from backend with express:module.exports = (req, res) => {res.status(200).sendFile(__d

I send a docx from backend with express:

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

I call it and download it as a blob from angular with:

 $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')
});

It works in Chrome and firefox. In safari a new tab opens, no file is downloaded. In IE (tested via Azure RemoteApp since I have a mac), I get " your current security settings do not allow this file to be downloaded".

SaveAs is from .js/

Is there an alternate way of doing thins that works in all modern browsers and IE10+?

I send a docx from backend with express:

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

I call it and download it as a blob from angular with:

 $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')
});

It works in Chrome and firefox. In safari a new tab opens, no file is downloaded. In IE (tested via Azure RemoteApp since I have a mac), I get " your current security settings do not allow this file to be downloaded".

SaveAs is from https://github./eligrey/FileSaver.js/

Is there an alternate way of doing thins that works in all modern browsers and IE10+?

Share Improve this question edited Aug 10, 2016 at 14:08 Joe asked Aug 10, 2016 at 13:30 JoeJoe 4,26432 gold badges106 silver badges180 bronze badges 6
  • Have you tried using arraybuffer instead of blob as the responseType? Also might want to check you have access to the headers, I know that sometimes you have to explicitly allow access with Access-Control-Expose-Headers – deadwards Commented Aug 10, 2016 at 13:33
  • Tried it. Same result. – Joe Commented Aug 10, 2016 at 13:40
  • Hmm... out of interest, what does the saveAs function do? I was doing something very similar yesterday, saving xlsx as a blob and it worked fine, albeit in Chrome. – deadwards Commented Aug 10, 2016 at 13:47
  • It saves the file. github./eligrey/FileSaver.js – Joe Commented Aug 10, 2016 at 13:52
  • Ah right, wasn't sure if it was something you had written yourself. – deadwards Commented Aug 10, 2016 at 13:52
 |  Show 1 more ment

1 Answer 1

Reset to default 2

Instead of using saveAs, could you try the following:

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();
}

And then removing the anchor when you're finished with it. Just wondering if it's the saving part that is the issue, I don't have much experience with FileSaver, but this is what I've done in the past

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744187600a4562281.html

相关推荐

  • javascript - Download docx as blob - Stack Overflow

    I send a docx from backend with express:module.exports = (req, res) => {res.status(200).sendFile(__d

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信