Using Compojure, Hiccup and Ring to upload a file
Asked Answered
H

2

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?

Hetrick answered 17/1, 2011 at 11:30 Comment(0)
D
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.

Dardan answered 17/1, 2011 at 16:56 Comment(2)
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?Hetrick
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"} ...]Dardan
A
1
[:input {:type "submit" :name "submit" :value "submit"]]

Missing }

[:input {:type "submit" :name "submit" :value "submit"]}]
Amylopsin answered 1/5, 2013 at 17:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.