I'm trying to download file, which is generated by server in maner as below:
public downloadZip(): void{
const link = document.createElement('a');
link.setAttribute(
'href',
`/srv/genZip?id=${this.id}`
);
link.setAttribute('download', '123.zip');
link.click();
}
But it doesn't work... Does anyone have an idea where I'm wrong?
You can't absolutely drive this process, you can only provide hints to the browser.
The browser has a couple of different places it may look for a filename for the download:
The server's Content-Disposition header, which can include a filename, for instanceContent-Disposition: attachment; filename="123.zip"
orContent-Disposition: attachment; filename*="123.zip"
As far as I can tell, there's no specification saying which should have preference, so to maximize the browsers across which your code will work, I'd specify the same name in both.
Underscoring that, there's this note in MDN's coverage of Content-Disposition above:
Note: Chrome, and Firefox 82 and later, prioritize the HTML
<a>element'sdownloadattribute over theContent-Disposition: inlineparameter (for same-origin URLs). Earlier Firefox versions prioritize the header and will display the content inline.
So if your server is returning Content-Disposition: inline and you're seeing the resource loaded inline (not downloaded), you may be using an older copy of Firefox or a browser that does the same thing.