Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you accept file uploads in noir

Tags:

clojure

noir

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?

like image 681
Stephen Olsen Avatar asked Feb 08 '12 02:02

Stephen Olsen


1 Answers

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

  • http://groups.google.com/group/clj-noir/browse_thread/thread/58e45ad02bb21571

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:

  • https://gist.github.com/1315692

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:

  • http://francesco-cek.com/uploading-a-file-to-amazon-s3-using-clojure-and-noir-2/

Hope this helps.

like image 58
icyrock.com Avatar answered Sep 23 '22 03:09

icyrock.com