Created
August 19, 2022 21:09
-
-
Save n8jadams/caa2ebe4f58f25e5c0d46aa04bc4c604 to your computer and use it in GitHub Desktop.
Download plaintext 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
async function downloadTextAsFile(text: string, filename: string): Promise<void> { | |
if (!filename.endsWith(".txt")) { | |
filename = `${filename}.txt`; | |
} | |
const blob = new Blob([text], { type: "text/plain" }); | |
const href = await URL.createObjectURL(blob); | |
const link = document.createElement("a"); | |
link.href = href; | |
link.download = filename; | |
link.style.position = "absolute"; | |
link.style.left = "200vw"; | |
document.body.appendChild(link); | |
link.click(); | |
document.body.removeChild(link); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment