Hi guys.
Continuing the conversation from here, is there a way to create downloadable files in HTML, i mean files that can be shown in the browser downloads?
Yes! First you need to obtain an URL to your downloaded file. If it’s something you just generated, like a screenshot, make sure it’s a data URL (like this: data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7
).
In JS (writing this from memory, so might not be 100% accurate):
- Create an
<a>
tag with something likeconst el = document.createElement('a')
- Set
el.href = yourUrl
- Set
el.download = 'yourfilename.gif'
. This might not work on all browsers, though. - Add it to
<body>
:document.body.append(el)
- Click it:
el.click()
. This might not work if not done from a user-generated JS event, likeonclick
. Defold’son_input
doesn’t count. You’ll need to find a way to attach it to a JS event. - Remove it from the DOM:
el.remove()
5 Likes