本文介绍了使用Compojure,Hiccup和Ring上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要上传一个文件到我在Clojure写的服务器,我需要一个客户端形式,看起来像这样:

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" />

然而,我找不到Hiccup的文档或Compojure中的文档来创建这样的表单。示例我看起来像这样:

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?

推荐答案

对于Compojure的上传支持可以在 multipart-params 环中间件中找到。以下是一些使用方法的示例:

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/

总是看看Ring中间件文档,它有很棒的代码!

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>

这应该可以做到:

[: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.

这篇关于使用Compojure,Hiccup和Ring上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 15:18