4

I am currently trying to trigger a file download using following code in Javascript: window.location.href = downloadUrl;

That works fine in Chrome, IE and Edge, but Firefox unloads the page due to the new URL and hence closes all opened websockets. I know that this is an odd mannerism of FF, but is there any workaround which I can use? It would work with window.open(downloadUrl); and closing the new tab after a certain timeout, but I would like to prevent opening a new tab just for triggering the download.

Any help would be appreciated, thanks.

bosboy
  • 113
  • 1
  • 9

2 Answers2

2

After a lot of researching and experimenting I found the following solution:

Create a link in JavaScript with the download attribute, click it and remove it after some time (I am using ExtJs):

var a = document.createElement("a");
document.body.appendChild(a);
a.style = 'display: none';
a.href = downloadUrl;
a.download = 'test.zip';
a.click();

Ext.defer(function(link) {
    document.body.removeChild(link);
}, 200, this, [a]);
bosboy
  • 113
  • 1
  • 9
  • Solution did not work for me, the websocket still closes as before. I tested `window.open( uri ).close();` but that doesn't work in Opera as it also closes the prompt asking which application to open the link in. – roskelld Aug 21 '19 at 05:44
  • I just tried the provided solution in my software, downloading files and keeping WebSockets open still works perfectly (in Firefox) using the temporary link. Firefox also opens a prompt asking which application to open the link in when downloading a file but that prompt doesn't close the websocket. – bosboy Aug 21 '19 at 09:14
  • I'm not familiar with ExtJs, but I'm guessing that it holds no power in this solution. With FireFox (68.0.2) I'm getting the prompt asking which application to open the uri in (I'm using `lightning://...`, but have also tested with `tg://`) but a frame or two after I get the Websocket disconnection message. Is there anything else outside of the posted code that you think might be missing? I've even tried this by just adding the link into my `HTML` as `LINK` With the same Websocket close issue. – roskelld Aug 21 '19 at 19:07
  • My solution as of now is to check if the browser is FireFox `if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){ //code }` and open the link via `window.open( url )` Not exactly happy about the solution, but it's working in my tests. – roskelld Aug 21 '19 at 20:30
2

Just stumbled on this issue. When user clicks a link (including 'download' one), browser believes he is leaving current page and closes websocket (this is likely reasonable). Opening link by Javascript onclick event (or window.open and so on) doesn't help. Setting target="_blank" helps, but creates uncomfortable blink at the moment of click. Finally I came up with the following:

<iframe src="about:blank" name="iframe_a" class="ws_keep_conn"></iframe>
<a href="https://www.fileserver.com/file.xml" target="iframe_a">Click this</a>

CSS

.ws_keep_conn {position: absolute; left: -9999px; visibility: hidden;}

That's all, websocket lives, smooth behavior with no blink!