From 6861163179035026012b8be48d27628a7939a0c2 Mon Sep 17 00:00:00 2001 From: Yatharth Malhotra <72160832+yatharthm22@users.noreply.github.com> Date: Sat, 26 Nov 2022 22:06:02 +0530 Subject: [PATCH] Download Button Download the contents as .txt file --- src/components/TextForm.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/components/TextForm.js b/src/components/TextForm.js index 07549d6..1f974e9 100644 --- a/src/components/TextForm.js +++ b/src/components/TextForm.js @@ -29,6 +29,20 @@ export default function TextForm(props) { navigator.clipboard.writeText(text); props.showAlert("Copied to Clipboard!", "success"); } + + const handleDownload = () => { + // file object + const file = new Blob([text], { type: "text/plain" }); + + // anchor link + const element = document.createElement("a"); + element.href = URL.createObjectURL(file); + element.download = "100ideas-" + Date.now() + ".txt"; + + // simulate link click + document.body.appendChild(element); // Required for this to work in FireFox + element.click(); + }; // Credits: Coding Wala const handleExtraSpaces = () => { @@ -51,6 +65,7 @@ export default function TextForm(props) { +