Button click download file and close window in javascript
Show 9 more comments. Gray k 22 22 gold badges silver badges bronze badges. Cfreak Cfreak In when the answer was originally posted it wasn't supported in any major browser. This doesn't necessarily trigger a download. It simply causes the browser to navigate to file. Whether that results in file. Show 3 more comments.
Ani Menon What if i wanna download a xml file? Thanks for your code. I have tested, it can working in IE, Chrome, Firefox. If you have a file acceptable by the browser like a PDF it will open in new tab instead to show download dialog. You could use window. Show 1 more comment. Stefanos Chrs Stefanos Chrs 1, 2 2 gold badges 14 14 silver badges 44 44 bronze badges.
NicholasKyriakides Kind of reminds me of this gem: image. BryanLarsen You are right, Firefox doesn't allow this without adding the element to body first. Thank you, updating the answer — Stefanos Chrs. Is there a way that javascript function be triggered once the download finishes? Just trying to show a message once downloads start and remove the message once download completes.
This is best solution out here and should be accepted answer. Show 6 more comments. Mark Amery k 65 65 gold badges silver badges bronze badges. Matt Ball Matt Ball k 95 95 gold badges silver badges bronze badges. Perfect, thanks. Do you happen to know if most servers will set the Content-Disposition to 'attachment' by default? There is no "most. Don't rely on it being set. This issue has been driving me ballistic, and this was the only option that worked and is supported by IE.
No jquery. This doest work if you're trying to download an image, it would open the image in the browser — Dheeraj. Danubian Sailor Danubian Sailor It does, at least if you actually apprnf the iframe to document.
This doesn't seem to be working in Chrome right now, although it used to work. I wonder if it kind of intermittently stops working in different versions of Chrome. Works in Chrome as of Version Writing code in comment? Please use ide.
Load Comments. What's New. Most popular in JavaScript. Convert a string to an integer in JavaScript Node. Most visited in Web Technologies. Installation of Node. Internet Explorer still does not supports the native ES6 promises, and Axios depends heavily on them. Download Algorithm Create a text area to enter the text data. This will help us to replace certain special characters with a combination of escape sequences.
Append this created element to the body of the document HTML page. Use element. Remove the element from the body of the document HTML page. At this time we make the hidden spinner DIV visible and allow it take over the screen note position absolute in style. After the file downloads, it is given to the user and the. All done with client side javascript! You can't do that using just client script, because there is no event for when the download completes.
You would have to download the file through a proxy page on the server, with a unique identity in the URL so that each download could be identified. Then you could send AJAX requests from the script to the server to determine the status of the download. This code works for jQuery, but with simple modification also for javascript. With this you won't need to change your requests-codes at all. This last line makes, that every request sent by your web application, executes the function "showAnimation" at the beginning and executes the function "hideAnimation", when the request is done.
Note: In case you have some frequent updating on your website, which you don't want to show that animation for, just make the displaying of the animation dependent from a global variable, which you set to false before sending those special requests, which you don't want the animation to be shown for. And don't forget to set it to true, when the request is done by them. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow.
0コメント