How do you accept file uploads in noir
Asked Answered
A

2

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?

Anaerobe answered 8/2, 2012 at 2:23 Comment(0)
W
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.

Wicopy answered 9/2, 2012 at 1:6 Comment(0)
C
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

Crying answered 30/7, 2012 at 15:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.