Skip to content

Instantly share code, notes, and snippets.

@webkupas
Forked from liabru/save-file-local.js
Created September 28, 2018 12:28
Show Gist options
  • Select an option

  • Save webkupas/cd3c8f65648d9e4d117ed6708f09b837 to your computer and use it in GitHub Desktop.

Select an option

Save webkupas/cd3c8f65648d9e4d117ed6708f09b837 to your computer and use it in GitHub Desktop.

Revisions

  1. @liabru liabru created this gist Apr 24, 2014.
    12 changes: 12 additions & 0 deletions save-file-local.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    /*
    * Save a text file locally with a filename by triggering a download
    */

    var text = "hello world",
    blob = new Blob([text], { type: 'text/plain' }),
    anchor = document.createElement('a');

    anchor.download = "hello.txt";
    anchor.href = (window.webkitURL || window.URL).createObjectURL(blob);
    anchor.dataset.downloadurl = ['text/plain', anchor.download, anchor.href].join(':');
    anchor.click();