Download file not working

  • A+

I am new to Javascript, I want to download a file that comes from a dynamic url after the result of a promise, it is a generated pdf which I am trying to download with the following call but unable to make it work as download doesn't start.

<button (click)='downloadMyFile()'>Download</button>  downloadMyFile(){   //url    .then((result)=>{    //result is contains a url     window.location.href = result })   .catch((error)=>{    //myerror }) } 

Here is plunk

You can force download file like this:

const link = document.createElement('a'); link.href = result; = 'download'; = '_blank';; 

Simply create anchor tag, set its href and download attributes and trigger click event.

Also note that this is not really about URL ending with extension or not - it is more about the headers that you send with the file response (namely Content-Type and Content-Disposition).


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: