使用Compojure、Hiccup和Ring上传文件

6
为了上传文件到我正在写的Clojure服务器,我需要一个客户端表单,看起来像这样:
<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创建此类表单的文档。我手头的样例看起来像这样:

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

我的问题是在哪里可以找到文档,了解如何修改以创建一个可以上传文件的表单?

2个回答

7
Compojure的文件上传支持可以在`multipart-params` Ring中间件中找到。以下是如何使用它的示例: 始终查看Ring中间件文档,其中包含许多很棒的代码!
更新:第一次没有正确阅读您的问题!要生成此表单:
<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"]]

我凭记忆完成了它,所以它未经测试。


谢谢,先生。那帮了我忙,使我得以运行。我仍然不确定我开始的示例为什么有input.math(我找不到关于math来自哪里的文档)。我也很难找到ring的文档,或者我需要使用autodoc来构建它吗? - justinhj
1
此外,.math 部分是将类属性添加到 HTML 元素的快捷方式。在 Hiccup 中,标签关键字可以以 CSS 选择器的方式增加 id 和 class 属性,例如:[:span#my_id.class1.class2 ...] 等同于 [:span {:id "my_id" :class "class1 class2"} ...] - Nicolas Buduroi

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接