Last active
January 31, 2017 09:41
-
-
Save pbaille/0051afc8ee28ea060a3c01c4ea707245 to your computer and use it in GitHub Desktop.
file uploader in cljs
This file contains hidden or 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 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