Fork me on GitHub
#clojurescript
<
2020-11-17
>
Andreas S.20:11:05

Hi! is it possible to load a local file with clojurescipt? Any ideas appreciated!

phronmophobic20:11:57

where are you running your clojurescript? in the browser? locally, with nodejs?

phronmophobic08:11:50

I would use javascript interop to get the file using the apis like https://web.dev/read-files/ . I would give a better answer but it’s pretty late here.

Andreas S.08:11:04

Thanks for answering! Could you sketch out how I would call this from cljs? Take your time, you don’t have so answer right away :)

tomrbowden10:11:32

Here’s a snippet that may help you (Reagent/Hiccup):

(defn get-file []
  (let [handle-change (fn [event]
                        (let [files (.. event -target -files)
                              file (first (js->clj files))]
                          (println file)))] ;; do something with file
    [:input {:type "file"
             :on-change handle-change}]))

Andreas S.20:11:14

hi @U0187JC6398 thank you for answering! lets say I have a "README.md" in the same directory as my cljs file, where would I put file name and or path information in your function?

phronmophobic21:11:14

to do that, you would have to use a clojure macro that reads the file at compile time and makes the data available when the clojurescript code is eventually run in the browser

Andreas S.08:11:53

Could I read the data into data script or something?

Andreas S.08:11:18

I mean if I turn my markdown files into clojurescript source code files, then it should work?