Last active
April 5, 2023 06:55
-
-
Save TheRealOwenRees/3ad1b98384b8bbf9e04066ae51af9431 to your computer and use it in GitHub Desktop.
Download any string as a file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const downloadString = (string, filename) => { | |
| try { | |
| const element = document.createElement("a"); | |
| const file = new Blob([string], { | |
| type: "text/plain", | |
| }); | |
| element.href = URL.createObjectURL(file); | |
| element.download = filename; | |
| document.body.appendChild(element); | |
| element.click(); | |
| } catch (error) { | |
| console.error(error); // TODO do something other than console.log | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment