Created
September 2, 2010 17:54
-
-
Save nikolaplejic/562624 to your computer and use it in GitHub Desktop.
File upload example in Compojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns fileupload.core | |
(:use [net.cgrand.enlive-html | |
:only [deftemplate defsnippet content clone-for | |
nth-of-type first-child do-> set-attr sniptest at emit*]] | |
[compojure.core] | |
[ring.adapter.jetty]) | |
(:require (compojure [route :as route]) | |
(ring.util [response :as response]) | |
(ring.middleware [multipart-params :as mp]) | |
(clojure.contrib [duck-streams :as ds])) | |
(:gen-class)) | |
(defn render [t] | |
(apply str t)) | |
(deftemplate index "fileupload/index.html" []) | |
(deftemplate upload-success "fileupload/success.html" []) | |
(defn upload-file | |
[file] | |
(ds/copy (file :tempfile) (ds/file-str "file.out")) | |
(render (upload-success))) | |
(defroutes public-routes | |
(GET "/" [] (render (index))) | |
(mp/wrap-multipart-params | |
(POST "/file" {params :params} (upload-file (get params "file"))))) | |
(defn start-app [] | |
(future (run-jetty (var public-routes) {:port 8000}))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<title>Compojure file upload</title> | |
</head> | |
<body> | |
<form action="/file" method="post" enctype="multipart/form-data"> | |
<input name="file" type="file" size="20" /> | |
<input type="submit" name="submit" value="submit" /> | |
</form> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<title>Compojure file upload</title> | |
</head> | |
<body> | |
<h1>File upload successful!</h1> | |
</body> | |
</html> |
I disagree. The basic concept still works. Here a simplified version of the copying
(mp/wrap-multipart-params
(POST "/fileupload" {:keys [params]}
(let [{:keys [tempfile filename]} (get params "file")]
(io/copy tempfile (java.io.File. (str root-directory "/" filename))))
(response "OK")))
I found compojure already wrap-multipart-params, so no need to wrap it again.
Below code works for me
(POST "/filestorage"
{{{tempfile :tempfile filename :filename} :img} :params :as params}
(io/copy tempfile (io/file filename)))
use luminus is quite easy , see my demo https://github.com/WeweTom/clojure-luminus-web-framework-upload-file-demo
@Wewetom, your example looks promising. How about handling multiple files?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As of Jan 2014 this is incredibly outdated.