Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blank window doesn't close automatically after download file

I have an Electron application that opens an external page. Inside this page, I have a button to download a file. On a normal browser, when I click the download button, it opens a _blank tab and start the download automatically, and immediately closes the tab. But, on Electron application, the _blank tab opens a new Window and ask for download location and the window don't close automatically, as desired.

How can I reproduce the "normal" browser behaviour on Electron?

The code to download the file is: window.open(url, '_blank', '');

Thank you

like image 428
rubenccreis Avatar asked Dec 29 '25 12:12

rubenccreis


1 Answers

What if you replace your code to download the file with a dummy link element:

var a = document.createElement("a");
a.href = url;
a.download = "filename";
a.click();

Does that result in the same behavior or could it be an acceptable workaround?

EDIT: Or perhaps even simpler; replace your download button with a link (that you can style as a button):

<a href="your-data-uri" download="filename">
like image 198
Vegard Løkken Avatar answered Jan 01 '26 01:01

Vegard Løkken



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!