mirror of
https://github.com/rwf2/Rocket.git
synced 2025-02-17 22:22:03 +00:00
Explicitly mention multipart support in guide.
This commit is contained in:
parent
2727d7bb7b
commit
dcbb1941c5
@ -761,15 +761,16 @@ struct Task<'r> {
|
|||||||
fn new(task: Form<Task<'_>>) { /* .. */ }
|
fn new(task: Form<Task<'_>>) { /* .. */ }
|
||||||
```
|
```
|
||||||
|
|
||||||
The [`Form`] type implements the `FromData` trait as long as its generic
|
[`Form`] is data guard as long as its generic parameter implements the
|
||||||
parameter implements the [`FromForm`] trait. In the example, we've derived the
|
[`FromForm`] trait. In the example, we've derived the `FromForm` trait
|
||||||
`FromForm` trait automatically for the `Task` structure. `FromForm` can be
|
automatically for `Task`. `FromForm` can be derived for any structure whose
|
||||||
derived for any structure whose fields implement [`FromForm`], or equivalently,
|
fields implement [`FromForm`], or equivalently, [`FromFormField`].
|
||||||
[`FromFormField`]. If a `POST /todo` request arrives, the form data will
|
|
||||||
automatically be parsed into the `Task` structure. If the data that arrives
|
If a `POST /todo` request arrives, the form data will automatically be parsed
|
||||||
isn't of the correct Content-Type, the request is forwarded. If the data doesn't
|
into the `Task` structure. If the data that arrives isn't of the correct
|
||||||
parse or is simply invalid, a customizable error is returned. As before, a
|
Content-Type, the request is forwarded. If the data doesn't parse or is simply
|
||||||
forward or failure can be caught by using the `Option` and `Result` types:
|
invalid, a customizable error is returned. As before, a forward or failure can
|
||||||
|
be caught by using the `Option` and `Result` types:
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
# use rocket::{post, form::Form};
|
# use rocket::{post, form::Form};
|
||||||
@ -779,6 +780,29 @@ forward or failure can be caught by using the `Option` and `Result` types:
|
|||||||
fn new(task: Option<Form<Task<'_>>>) { /* .. */ }
|
fn new(task: Option<Form<Task<'_>>>) { /* .. */ }
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Multipart
|
||||||
|
|
||||||
|
Multipart forms are handled transparently, with no additional effort. Most
|
||||||
|
`FromForm` types can parse themselves from the incoming data stream. For
|
||||||
|
example, here's a form and route that accepts a multipart file upload using
|
||||||
|
[`TempFile`]:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
# #[macro_use] extern crate rocket;
|
||||||
|
|
||||||
|
use rocket::form::Form;
|
||||||
|
use rocket::fs::TempFile;
|
||||||
|
|
||||||
|
#[derive(FromForm)]
|
||||||
|
struct Upload<'r> {
|
||||||
|
save: bool,
|
||||||
|
file: TempFile<'r>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[post("/upload", data = "<upload>")]
|
||||||
|
fn upload_form(upload: Form<Upload<'_>>) { /* .. */ }
|
||||||
|
```
|
||||||
|
|
||||||
[`Form`]: @api/rocket/form/struct.Form.html
|
[`Form`]: @api/rocket/form/struct.Form.html
|
||||||
[`FromForm`]: @api/rocket/form/trait.FromForm.html
|
[`FromForm`]: @api/rocket/form/trait.FromForm.html
|
||||||
[`FromFormField`]: @api/rocket/form/trait.FromFormField.html
|
[`FromFormField`]: @api/rocket/form/trait.FromFormField.html
|
||||||
|
Loading…
Reference in New Issue
Block a user