Created
May 25, 2017 21:47
-
-
Save noisesmith/28f61a165d044ce45a82935f04d61946 to your computer and use it in GitHub Desktop.
create a download of some data in a clojurescript repl (eg. figwheel)
This file contains 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
(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