Skip to content

Instantly share code, notes, and snippets.

@lewdev
Created April 12, 2023 00:52
Show Gist options
  • Select an option

  • Save lewdev/e36e0e985f55f1f1251c06969b684d23 to your computer and use it in GitHub Desktop.

Select an option

Save lewdev/e36e0e985f55f1f1251c06969b684d23 to your computer and use it in GitHub Desktop.
Download text to a file (đź”–Bookmarklet)

Download text to a file

Code that will take a filename and text data and have the user download it.

Run it in your address bar (Bookmarklet)

data:text/html,<input id="filenameInput" value="file-to-save.txt"/><button onclick="downloadToFile(filenameInput.value, contentInput.value)">Download File</button><textarea id="contentInput" style="width:100%" rows="10">Content</textarea><script>const downloadToFile=(filename,content)=>{if(!content){alert("No content to save!");return;}
const a=document.createElement("a");const file=new Blob([content],{type:"text/plain"});a.href=URL.createObjectURL(file);a.download=filename;a.click();URL.revokeObjectURL(a.href);};</script>
<input id="filenameInput" value="file-to-save.txt"/>
<button onclick="downloadToFile(filenameInput.value, contentInput.value)">Download File</button>
<br><br>
<textarea id="contentInput" style="width:100%" rows="10">Content</textarea>
<script>
const downloadToFile = (filename, content) => {
if (!content) { alert("No content to save!"); return; }
const a = document.createElement("a");
const file = new Blob([content], { type: "text/plain" });
a.href = URL.createObjectURL(file);
a.download = filename;
a.click();
URL.revokeObjectURL(a.href);
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment