Skip to content

Instantly share code, notes, and snippets.

@noisesmith
Created May 25, 2017 21:47
Show Gist options
  • Save noisesmith/28f61a165d044ce45a82935f04d61946 to your computer and use it in GitHub Desktop.
Save noisesmith/28f61a165d044ce45a82935f04d61946 to your computer and use it in GitHub Desktop.
create a download of some data in a clojurescript repl (eg. figwheel)
(defn download
; adapted from https://stackoverflow.com/a/3665147
[data filename encoder]
(let [el (.createElement js/document "a")
data-str (js/encodeURIComponent (encoder data))]
(doto el
(.setAttribute "href" (str "data:text/plain;charset=utf-8,"
data-str))
(.setAttribute "download" filename))
(aset (.-style el) "display" "none")
(.appendChild (.-body js/document) el)
(.click el)
(.removeChild (.-body js/document) el)
true))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment