Using Compojure, Hiccup and Ring to upload a file

青春壹個敷衍的年華 提交于 2019-12-06 03:23:29

问题


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?


回答1:


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

  • https://gist.github.com/562624/1df418e4851e68952fc466713f377df2e653afdb
  • http://www.prodevtips.com/2010/12/19/file-uploads-with-clojure-ring-and-compojure/

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.




回答2:


[:input {:type "submit" :name "submit" :value "submit"]]

Missing }

[:input {:type "submit" :name "submit" :value "submit"]}]


来源:https://stackoverflow.com/questions/4712645/using-compojure-hiccup-and-ring-to-upload-a-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!