Skip to content

Instantly share code, notes, and snippets.

@zoren
Created June 18, 2020 15:21
Show Gist options
  • Save zoren/cc74758198b503b1755b75d1a6b376e7 to your computer and use it in GitHub Desktop.
Save zoren/cc74758198b503b1755b75d1a6b376e7 to your computer and use it in GitHub Desktop.
Download a BLOB via a object URL in ClojureScript
;; heavily inspired by Mariano Guerra
;; http://marianoguerra.org/posts/download-frontend-generated-data-to-a-file-with-clojurescript.html
(defn download-blob [file-name blob]
(let [object-url (js/URL.createObjectURL blob)
anchor-element
(doto (js/document.createElement "a")
(-> .-href (set! object-url))
(-> .-download (set! file-name)))]
(.appendChild (.-body js/document) anchor-element)
(.click anchor-element)
(.removeChild (.-body js/document) anchor-element)
(js/URL.revokeObjectURL object-url)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment