Search code examples
clojureclojurescript

How to get files from <input type='file' …/> in ClojureScript


I'm trying to get image file in input field but I could not do it. Here is the code:

:on-change (fn [_]
             (this-as this
               (println "Files: " (.-files this))))

But (.-files this) returns nil.

Any ideas?

P.S: I would like to upload this image to my server.


Solution

  • Here is a workable snippet from our project:

    :on-change
    (fn [this]
        (if (not (= "" (-> this .-target .-value)))
            (let [^js/File file (-> this .-target .-files (aget 0))]
              ;; your logic here ...
              ;; now reset the widget to let user upload a new one
              (set! (-> this .-target .-value) ""))))
    

    Hope this would help.