Skip to content

Instantly share code, notes, and snippets.

@TheRealOwenRees
Last active April 5, 2023 06:55
Show Gist options
  • Select an option

  • Save TheRealOwenRees/3ad1b98384b8bbf9e04066ae51af9431 to your computer and use it in GitHub Desktop.

Select an option

Save TheRealOwenRees/3ad1b98384b8bbf9e04066ae51af9431 to your computer and use it in GitHub Desktop.
Download any string as a file
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