How to create a dynamic file + link for download in Javascript? [duplicate]

Here’s a solution I’ve created, that allows you to create and download a file in a single click: <html> <body> <button onclick=’download_file(“my_file.txt”, dynamic_text())’>Download</button> <script> function dynamic_text() { return “create your dynamic text here”; } function download_file(name, contents, mime_type) { mime_type = mime_type || “text/plain”; var blob = new Blob([contents], {type: mime_type}); var dlink = document.createElement(‘a’); … Read more