Vikash Anand
Vikash Anand

Reputation: 67

Set the file name and open blob word type

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

Upvotes: 3

Views: 10928

Answers (1)

Nimer Awad
Nimer Awad

Reputation: 4199

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

Upvotes: 5

Related Questions