Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions forms/multipart/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,26 @@ cd forms/multipart
cargo run
```

Open <http://localhost:8080> in your browser and upload one or more files.
Open <http://localhost:8080> in your browser and upload one or more files. The
form also includes an optional `pub_time` text field. The handler checks
`form.files.is_empty()` to detect missing file parts and reads
`form.pub_time` as `Option<Text<String>>`.

You can submit the same fields with `curl`:

```sh
curl -X POST http://localhost:8080/ \
-F 'pub_time=2026-12-06 12:00' \
-F 'file=@/path/to/first-file' \
-F 'file=@/path/to/second-file'
```

Omit both `file` parts to exercise the no-file case, or omit `pub_time` to
exercise the optional text field.

## Result

The files are saved in `forms/multipart/tmp`.
The files are saved in `forms/multipart/tmp`; saved file paths and the
publication time are logged.

This example uses `unwrap` for file persistence and can panic if a file cannot be saved.
11 changes: 11 additions & 0 deletions forms/multipart/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use actix_multipart::{
form::{
MultipartForm,
tempfile::{TempFile, TempFileConfig},
text::Text,
},
};
use actix_web::{App, Error, HttpResponse, HttpServer, Responder, middleware, web};
Expand All @@ -15,11 +16,20 @@ use uuid::Uuid;
struct UploadForm {
#[multipart(rename = "file")]
files: Vec<TempFile>,
pub_time: Option<Text<String>>,
}

async fn save_files(
MultipartForm(form): MultipartForm<UploadForm>,
) -> Result<impl Responder, Error> {
if form.files.is_empty() {
log::info!("No file fields were submitted");
}

if let Some(pub_time) = &form.pub_time {
log::info!("Publication time: {}", pub_time.0);
}

for f in form.files {
let path = format!("./tmp/{}", f.file_name.unwrap());
log::info!("Saving to {path}");
Expand All @@ -35,6 +45,7 @@ async fn index() -> HttpResponse {
<body>
<form target="/" method="post" enctype="multipart/form-data">
<input type="file" multiple name="file"/>
<input type="text" name="pub_time" placeholder="Publication time"/>
<button type="submit">Submit</button>
</form>
</body>
Expand Down