6

To upload a file to a server I'm writing in Clojure I need a client form that looks something like this:

<form action="/file" method="post" enctype="multipart/form-data">
<input name="file" type="file" size="20" />
<input type="submit" name="submit" value="submit" />

However I can't find the documentation for Hiccup or in Compojure to create a form like this. The sample I have looks like this :

[:h2 "Choose a file to upload"]
:form {:method "post" :action "/upload"}
[:input.math {:type "text" :name "a"}] [:span.math " + "]
[:input.math {:type "text" :name "b"}] [:br]

So my question is where is the documentation to find how this should be modified to make a form that will upload a file?

Kenny Evitt
  • 9,291
  • 5
  • 65
  • 93
justinhj
  • 11,147
  • 11
  • 58
  • 104

2 Answers2

7

The file upload support for Compojure can be found in the multipart-params Ring middleware. Here's some examples of how to use it:

Always have a look at Ring middleware documentation, it is full of great code!

Update: Didn't read your question right the first time! To generate a form like this one:

<form action="/file" method="post" enctype="multipart/form-data">
  <input name="file" type="file" size="20" />
  <input type="submit" name="submit" value="submit" />
</form>

That should do the trick:

[:form {:action "/file" :method "post" :enctype "multipart/form-data"}
 [:input {:name "file" :type "file" :size "20"}]
 [:input {:type "submit" :name "submit" :value "submit"]]

I've done it from memory, so it's untested.

Nicolas Buduroi
  • 3,565
  • 1
  • 28
  • 29
  • Thank you Sir, that helped me get running. I'm still not sure why the example I started with has input.math (I can't find documentation on where the math comes from). I also have a hard time finding the ring documentation, or do I need to just build it with autodoc? – justinhj Jan 17 '11 at 19:31
  • 1
    Also the .math part is a shortcut to add a class attribute to HTML elements. In Hiccup, tag keywords can be augmented with id and class attribute in a CSS selector manner, e.g.: `[:span#my_id.class1.class2 ...]` is equivalent to `[:span {:id "my_id" :class "class1 class2"} ...]` – Nicolas Buduroi Jan 17 '11 at 21:57
1
[:input {:type "submit" :name "submit" :value "submit"]]

Missing }

[:input {:type "submit" :name "submit" :value "submit"]}]
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
markus
  • 1
  • 1