Skip to content

Instantly share code, notes, and snippets.

@simongray
Created June 17, 2019 10:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simongray/4c404932da39afb101824b501fa5e509 to your computer and use it in GitHub Desktop.
Save simongray/4c404932da39afb101824b501fa5e509 to your computer and use it in GitHub Desktop.
Conversion functions between DataURL and Blob type
;; Based on the implementations found here: https://gist.github.com/wuchengwei/b7e1820d39445f431aeaa9c786753d8e
(defn dataurl->blob
[dataurl]
(let [arr (str/split dataurl #",")
mime (second (re-find #":(.*?);" (first arr)))
bstr (js/atob (second arr))
n (count bstr)
u8arr (-> (map (fn [i] (.charCodeAt bstr i)) (range n))
(into-array)
(js/Uint8Array.from))]
(js/Blob. (js/Array. u8arr) (js-obj "type" mime))))
(defn blob->dataurl
[blob callback]
(let [a (js/FileReader.)]
(set! (.-onload a)
(fn [e] (callback (-> e .-target .-result))))
(.readAsDataURL a blob)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment