Skip to content

File Uploads

Sebastian edited this page Dec 15, 2017 · 5 revisions

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 in wrap-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

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 file=@words.txt

This adds a file key to the :params map of the request where :tempfile is a object that contains the uploaded data. You can use this to perform any further processing you want.

  {"file" {:filename     "words.txt"
           :content-type "text/plain"
           :tempfile     #object[ ...]
           :size         51}}
You can’t perform that action at this time.