Skip to content

Instantly share code, notes, and snippets.

@pbaille
Last active January 31, 2017 09:41
Show Gist options
  • Select an option

  • Save pbaille/0051afc8ee28ea060a3c01c4ea707245 to your computer and use it in GitHub Desktop.

Select an option

Save pbaille/0051afc8ee28ea060a3c01c4ea707245 to your computer and use it in GitHub Desktop.
file uploader in cljs
(defn file-input [on-result]
[:input {:type "file"
:on-change
(fn [e]
(let [f (first (array-seq (.. e -target -files)))
reader (js/FileReader.)]
(aset reader "onload"
(fn [e]
(on-result (.. e -target -result))))
(.readAsText reader f)))}])
;; same as above but with just a button
(defn $ [sel]
(.querySelector js/document sel))
(defn file-input*
[{:keys [on-result text]}]
(let [id (str (gensym))]
(fn []
[:span
[:button {:on-click #(.click ($ (str "#" id)))} text]
[:input {:id id
:type "file"
:style {:display :none}
:on-change
(fn [e]
(let [f (first (array-seq (.. e -target -files)))
reader (js/FileReader.)]
(aset reader "onload"
(fn [e]
(on-result (.. e -target -result))))
(.readAsText reader f)))}]])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment