Created
June 17, 2019 10:52
-
-
Save simongray/4c404932da39afb101824b501fa5e509 to your computer and use it in GitHub Desktop.
Conversion functions between DataURL and Blob type
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
;; 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