Download file when page loads with vanilla js
I needed this behavior without explicit clicking since I want to trigger the download automatically at some point from js. If you wanted to pretty print the JSON, per this answer , you could use:.
I recently had to create a button that would download a json file of all values of a large form. This is what I did:. There was one issue with filename and extension in edge but at the time of writing this seemed to be a bug with Edge that is due to be fixed. This is adapted from maia's version, updated for modern Javascript with the deprecated initMouseEvent replaced by new MouseEvent and the code generally improved:.
The download property of links is new and not is supported in Internet Explorer see the compatibility table here. For a cross-browser solution to this problem I would take a look at FileSaver. You can easily auto download file with using Blob and transfer it in first param downloadJsonFile. How are we doing? Please help us improve Stack Overflow. Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 8 years ago. Active 2 months ago. Viewed k times. I have the following code to let users download data strings in csv file.
Am I missing something in my code? Thanks for reading my question:. Improve this question. Community Bot 1 1 1 silver badge. Eugene Yu Eugene Yu 3, 4 4 gold badges 20 20 silver badges 27 27 bronze badges.
Add a comment. Active Oldest Votes. This approach has the following advantages over other proposed ones: No HTML element needs to be clicked Result will be named as you want it no jQuery needed I needed this behavior without explicit clicking since I want to trigger the download automatically at some point from js. Improve this answer. Because you prepended data: — rjurney.
Can somebody point me to a spec or MDN page that explains in more detail how this whole prepended data-type thing works ie. I am using this, but it feels like magic, would be great to read up on the details but I don't even know how to google for it. This doesn't work in IE though when you have to download a big file with more than characters. This will not work without limits, though. You can only download about 1MB of data.
Show 10 more comments. Found an answer. Cybear can you explain the third line to me? You will want to prepend data: to your url, otherwise the user is likely to hit a character limit in many browsers. Hey , I know it is an old answer but, know this solution doesn't work on IE all of them IE does not familiar with download attribute reference - link — Ayalon Grinfeld. Gautham Gautham 7 7 silver badges 13 13 bronze badges. I meant to say, no need to deal with the HTML elements directly.
This is the best answer since it does not have a 1MB size limit and uses a library instead of custom hacks — oseiskar. Thanks for the response even after it's been 2yrs since I asked this question! Collectives on Stack Overflow. Learn more. Is it possible to force download a file like pdf as soon as a page loads Ask Question. Asked 6 years, 2 months ago. Active 6 years, 2 months ago. Viewed 6k times. Improve this question. I don't want any link to be clicked.
Pdf should download as soon as thank you page opens. Download by clicking a link is already working. Show 1 more comment. Active Oldest Votes. Improve this answer. Add a comment. Aminadav Glickshtein Aminadav Glickshtein 19k 11 11 gold badges 68 68 silver badges bronze badges. Does solution open "Save as" dialog?
Please let us know if this is what you need — Aminadav Glickshtein. Broken link on the Github example. You can use ajax to hit the url when the page loads. Community Bot 1 1 1 silver badge. Imab Asghar Imab Asghar 1 1 silver badge 7 7 bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog.
Who owns this outage?
0コメント