如何从 ClojureScript 中的 <input type='file' .../> 获取文件

Ert*_*tin 1 clojure clojurescript

我正在尝试在输入字段中获取图像文件,但我无法做到。这是代码:

:on-change (fn [_]
             (this-as this
               (println "Files: " (.-files this))))
Run Code Online (Sandbox Code Playgroud)

(.-files this)返回nil

有任何想法吗?

PS:我想把这张图片上传到我的服务器。

Iva*_*aev 5

这是我们项目中一个可行的片段:

: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) ""))))
Run Code Online (Sandbox Code Playgroud)

希望这会有所帮助。