Client-side middleware that can be used with HTTP remotes so that mutations can attach file uploads to mutation parameters.
Client-side middleware that can be used with HTTP remotes so that mutations can attach file uploads to mutation parameters.
(attach-uploads params objects-to-upload)
Attach js Blob or ArrayBuffer objects to the params
. This requires that you use http-remote
and that you
also install wrap-file-upload
middleware. If you use js/File objects then the filenames of those objects
will be available to the mutations on the server.
Example usage:
(let [uploads [(file-upload/new-upload "test" some-js-file)
(file-upload/new-upload "other" other-js-file)]]
(comp/transact! this [(some-mutation (attach-uploads {} uploads))]))
If you are using a browser file input, you can use evt->uploads
:
(dom/input {:type "file"
:multiple true
:onChange (fn [evt]
(let [uploads (file-upload/evt->uploads evt)]
(comp/transact! this [(some-mutation (file-upload/attach-uploads {} uploads))])))})
Attach js Blob or ArrayBuffer objects to the `params`. This requires that you use `http-remote` and that you also install `wrap-file-upload` middleware. If you use js/File objects then the filenames of those objects will be available to the mutations on the server. Example usage: ``` (let [uploads [(file-upload/new-upload "test" some-js-file) (file-upload/new-upload "other" other-js-file)]] (comp/transact! this [(some-mutation (attach-uploads {} uploads))])) ``` If you are using a browser file input, you can use `evt->uploads`: ``` (dom/input {:type "file" :multiple true :onChange (fn [evt] (let [uploads (file-upload/evt->uploads evt)] (comp/transact! this [(some-mutation (file-upload/attach-uploads {} uploads))])))}) ```
(evt->uploads file-input-change-event)
(evt->uploads file-input-change-event content-type)
Converts a file input onChange event into a sequence upload objects that are compatible with attach-uploads
.
If you want to manually set the content type of any item, then add a :file/content-type
key/value pair to the
returned uploads (which are just clojure maps), or pass a content-type argument to have that content type applied
to ALL of the uploads. NOTE: some server middleware can mis-interpret certain MIME types and open Readers on them
instead of byte streams, leading to file corruption of the uploaded file. You can try forcing the MIME type to
application/octet-stream
to overcome this.
Converts a file input onChange event into a sequence upload objects that are compatible with `attach-uploads`. If you want to manually set the content type of any item, then add a `:file/content-type` key/value pair to the returned uploads (which are just clojure maps), or pass a content-type argument to have that content type applied to ALL of the uploads. NOTE: some server middleware can mis-interpret certain MIME types and open Readers on them instead of byte streams, leading to file corruption of the uploaded file. You can try forcing the MIME type to `application/octet-stream` to overcome this.
(new-upload name content)
(new-upload name content content-type)
Create a new upload object from a string name and a js object (Blob, ArrayBuffer, or File). The resulting map is
safe to store in app state. If content-type
is supplied then the file upload support will attempt to force the
content type to the one provided. Normally js File objects will auto-set their MIME type, but this can sometimes be
mis-interpreted by server MIME configurations.
See attach-uploads
.
Create a new upload object from a string name and a js object (Blob, ArrayBuffer, or File). The resulting map is safe to store in app state. If `content-type` is supplied then the file upload support will attempt to force the content type to the one provided. Normally js File objects will auto-set their MIME type, but this can sometimes be mis-interpreted by server MIME configurations. See `attach-uploads`.
(wrap-file-upload handler)
(wrap-file-upload handler transit-options)
Adds support for attaching uploads to the parameters of any mutation.
transit-options
- A map of options to be included when converting the mutation and params for transmission. See
transit/transit-clj->str
. Use this to extend the transit support. This is necessary because
the regular request middleware will not be used to send transactions that include file uploads,
so any extensions to transit must be done in both places.
NOTE: This middleware acts as the end of the chain when it detects the need for a file upload, and rewrites the body, method, and clears any content-type header. As such, it should be used in the middleware so that it will be executed first:
(def client-middleware
(->
(net/wrap-fulcro-request)
(file-upload/wrap-file-upload)
...))
Adds support for attaching uploads to the parameters of any mutation. `transit-options` - A map of options to be included when converting the mutation and params for transmission. See `transit/transit-clj->str`. Use this to extend the transit support. This is necessary because the regular request middleware will not be used to send transactions that include file uploads, so any extensions to transit must be done in both places. NOTE: This middleware acts as the end of the chain when it detects the need for a file upload, and rewrites the body, method, and clears any content-type header. As such, it should be used in the middleware so that it will be executed first: ``` (def client-middleware (-> (net/wrap-fulcro-request) (file-upload/wrap-file-upload) ...)) ```
(wrap-mutation-file-uploads handler transit-options)
Middleware that enables the server to handle mutations that have attached file uploads to their parameters. This middleware must be composed after Ring middleware for multipart (required), but before the API handling.
For example:
(->
...
(wrap-api "/api")
(wrap-file-uploads)
(wrap-keyword-params)
(wrap-multipart-params)
...)
Middleware that enables the server to handle mutations that have attached file uploads to their parameters. This middleware must be composed after Ring middleware for multipart (*required*), but before the API handling. For example: ``` (-> ... (wrap-api "/api") (wrap-file-uploads) (wrap-keyword-params) (wrap-multipart-params) ...) ```
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close