I'm trying to running some codes from the book "Web development with Clojure". There is a function which I can not understand:
(defn handle-upload [{:keys [filename] :as file}]
(upload-page
(if (empty? filename)
"please select a file to upload"
(try
(upload-file (gallery-path) file)
(save-thumbnail file)
(db/add-image (session/get :user) filename)
(image {:height "150px"}
(str "/img/"
(session/get :user)
"/"
thumb-prefix
(url-encode filename)))
(catch Exception ex
(str "error uploading file " (.getMessage ex)))))))
where
(defn upload-page [info]
(layout/common
[:h2 "Upload an image"]
[:p info]
(form-to {:enctype "multipart/form-data"}
[:post "/upload"]
(file-upload :file)
(submit-button "upload"))))
What is the meaning of the parameter of the function handle-upload
?
And after the changing from
(defn handle-upload [{:keys [filename] :as file}] ...
to
(defn handle-upload [{:keys filename :as file}] ...
I got an error message:
java.lang.IllegalArgumentException: Don't know how to create ISeq from: clojure.lang.Symbol, compiling:(picture_gallery/routes/upload.clj:32:1)
Why?
{:keys [filename] :as file}
means:
:filename
key from passed argument and bind its value to filename
file
So if you pass:
{:filename "foo"
:somethingelse "bar"}
As an argument, then filename
in the function scope will be equal to foo
and the file
will be equal to the whole hash map.
References:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.