6

I have a file input setup like this

[:p "Upload a book"]
      (form-to [:post "/upload"]
               (file-upload :book)
               (submit-button "Upload"))

My upload endpoint then looks like this.

(defpage [:post "/upload"] {:keys [book]} (println book))

book just seems to be a string of the title of the file that was uploaded and not the file itself. How do I get the file?

animuson
  • 50,765
  • 27
  • 132
  • 142
Stephen Olsen
  • 643
  • 1
  • 5
  • 13

2 Answers2

4

According to this thread (see second post by Chris Granger):

you can use something like:

(defpage [:post "upload"] {:keys [myFile]}
  (println myFile) ;; see all the things the file contains
  (io/copy (io/file (:tempfile myFile)) (io/file "uploads/some-new-name"))) 

Here's a gist from this thread:

with a note (again from Chris) that you need Leiningen 1.6.1.1+ not to run into a bug.

You can see a similar thing (though for Amazon S3) here:

Hope this helps.

FCeccon
  • 128
  • 5
icyrock.com
  • 25,648
  • 4
  • 58
  • 78
2

I think you're accepting fine; I believe you're posting wrong. Try form-to {:enctype "multipart/form-data"}, or from the shell curl --form book=@/home/me/Penguins.jpg http://localhost:8080/Upload

Dax Fohl
  • 10,006
  • 5
  • 40
  • 82