-
Notifications
You must be signed in to change notification settings - Fork 524
File Uploads
Uploading a file to a website requires multipart form handling, which Ring provides with its wrap-multipart-params
middleware.
(wrap-multipart-params handler)
(wrap-multipart-params handler options)
The options for this middleware function are:
-
:encoding
The character encoding of the parameters. Acts the same as the same option inwrap-params
. -
:store
A function to use to store the uploaded files. There are two stores included with Ring.
A full example:
(require '[ring.middleware.params :refer [wrap-params]]
'[ring.middleware.multipart-params :refer [wrap-multipart-params]])
(def app
(-> your-handler
wrap-params
wrap-multipart-params))
By default, uploads are stored in temporary files that are deleted an hour after being uploaded. This is handled by ring.middleware.multipart-params.temp-file/temp-file-store function.
For example,
curl -XPOST "http://localhost:3000" -F [email protected]
This adds a file
key to the :params
map of the request where :tempfile
is a java.io.File
object that contains the uploaded data. You can use this to perform any further processing you want.
{...
:params
{"file" {:filename "words.txt"
:content-type "text/plain"
:tempfile #object[java.io.File ...]
:size 51}}
...}