javascript - Set the file name and open blob word type - Stack Overflow

I am trying to download word document, it is working but i am not how to set file name. Every time it i

I am trying to download word document, it is working but i am not how to set file name. Every time it is downloading with some unique name.

 this.downloadData("downloadVehicleLine").subscribe(
      data => {
       this.downLoadFile(data);   
      })

downLoadFile(data: any) {
    var blob = new Blob([data], { type: 'application/octet-stream' });
    var url = window.URL.createObjectURL(blob);
    var pwa = window.open(url, "createdocument.docx");
    if (!pwa || pwa.closed || typeof pwa.closed == 'undefined') {
        alert('Please disable your Pop-up blocker and try again.');
     }

}

It should download word document with the name createdocument.docx

I am trying to download word document, it is working but i am not how to set file name. Every time it is downloading with some unique name.

 this.downloadData("downloadVehicleLine").subscribe(
      data => {
       this.downLoadFile(data);   
      })

downLoadFile(data: any) {
    var blob = new Blob([data], { type: 'application/octet-stream' });
    var url = window.URL.createObjectURL(blob);
    var pwa = window.open(url, "createdocument.docx");
    if (!pwa || pwa.closed || typeof pwa.closed == 'undefined') {
        alert('Please disable your Pop-up blocker and try again.');
     }

}

It should download word document with the name createdocument.docx

Share Improve this question edited Dec 26, 2019 at 8:43 Nimer Awad 4,2093 gold badges19 silver badges34 bronze badges asked Jan 29, 2019 at 7:59 Vikash AnandVikash Anand 671 gold badge5 silver badges11 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

I think you can use an anchor element reference instead of window.URL; because you can set the name of the file to download property of the element.

You can use this code to download files:

var blob = new Blob([data], { type: 'application/octet-stream' });
var link = document.createElement('a');
link.href = URL.createObjectURL(blob);
// set the name of the file
link.download = "createdocument.docx";
// clicking the anchor element will download the file
link.click();

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

相关推荐

  • javascript - Set the file name and open blob word type - Stack Overflow

    I am trying to download word document, it is working but i am not how to set file name. Every time it i

    9天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信