1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-08-31 00:50:20 +02:00

fix: increase total limit in multipart example (#3567)

* fix: increase total limit in multipart example

* update readme

* Clarify where the limit comes from

* Fix link

---------

Co-authored-by: Yuki Okushi <huyuumi.dev@gmail.com>
This commit is contained in:
宮水_五葉
2025-08-22 13:38:51 +08:00
committed by GitHub
parent 971bbc1468
commit 37203c7fd8
3 changed files with 49 additions and 21 deletions

View File

@@ -24,9 +24,10 @@ Due to additional requirements for `multipart/form-data` requests, the higher le
## Examples ## Examples
```rust ```rust
use actix_web::{post, App, HttpServer, Responder}; use actix_multipart::form::{
json::Json as MpJson, tempfile::TempFile, MultipartForm, MultipartFormConfig,
use actix_multipart::form::{json::Json as MpJson, tempfile::TempFile, MultipartForm}; };
use actix_web::{middleware::Logger, post, App, HttpServer, Responder};
use serde::Deserialize; use serde::Deserialize;
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
@@ -36,25 +37,37 @@ struct Metadata {
#[derive(Debug, MultipartForm)] #[derive(Debug, MultipartForm)]
struct UploadForm { struct UploadForm {
// Note: the form is also subject to the global limits configured using `MultipartFormConfig`.
#[multipart(limit = "100MB")] #[multipart(limit = "100MB")]
file: TempFile, file: TempFile,
json: MpJson<Metadata>, json: MpJson<Metadata>,
} }
#[post("/videos")] #[post("/videos")]
pub async fn post_video(MultipartForm(form): MultipartForm<UploadForm>) -> impl Responder { async fn post_video(MultipartForm(form): MultipartForm<UploadForm>) -> impl Responder {
format!( format!(
"Uploaded file {}, with size: {}", "Uploaded file {}, with size: {}\ntemporary file ({}) was deleted\n",
form.json.name, form.file.size form.json.name,
form.file.size,
form.file.file.path().display(),
) )
} }
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
HttpServer::new(move || App::new().service(post_video)) env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
.bind(("127.0.0.1", 8080))?
.run() HttpServer::new(move || {
.await App::new()
.service(post_video)
.wrap(Logger::default())
// Also increase the global total limit to 100MiB.
.app_data(MultipartFormConfig::default().total_limit(100 * 1024 * 1024))
})
.workers(2)
.bind(("127.0.0.1", 8080))?
.run()
.await
} }
``` ```

View File

@@ -1,4 +1,6 @@
use actix_multipart::form::{json::Json as MpJson, tempfile::TempFile, MultipartForm}; use actix_multipart::form::{
json::Json as MpJson, tempfile::TempFile, MultipartForm, MultipartFormConfig,
};
use actix_web::{middleware::Logger, post, App, HttpServer, Responder}; use actix_web::{middleware::Logger, post, App, HttpServer, Responder};
use serde::Deserialize; use serde::Deserialize;
@@ -9,6 +11,7 @@ struct Metadata {
#[derive(Debug, MultipartForm)] #[derive(Debug, MultipartForm)]
struct UploadForm { struct UploadForm {
// Note: the form is also subject to the global limits configured using `MultipartFormConfig`.
#[multipart(limit = "100MB")] #[multipart(limit = "100MB")]
file: TempFile, file: TempFile,
json: MpJson<Metadata>, json: MpJson<Metadata>,
@@ -28,9 +31,15 @@ async fn post_video(MultipartForm(form): MultipartForm<UploadForm>) -> impl Resp
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
HttpServer::new(move || App::new().service(post_video).wrap(Logger::default())) HttpServer::new(move || {
.workers(2) App::new()
.bind(("127.0.0.1", 8080))? .service(post_video)
.run() .wrap(Logger::default())
.await // Also increase the global total limit to 100MiB.
.app_data(MultipartFormConfig::default().total_limit(100 * 1024 * 1024))
})
.workers(2)
.bind(("127.0.0.1", 8080))?
.run()
.await
} }

View File

@@ -13,7 +13,7 @@
//! ```no_run //! ```no_run
//! use actix_web::{post, App, HttpServer, Responder}; //! use actix_web::{post, App, HttpServer, Responder};
//! //!
//! use actix_multipart::form::{json::Json as MpJson, tempfile::TempFile, MultipartForm}; //! use actix_multipart::form::{json::Json as MpJson, tempfile::TempFile, MultipartForm, MultipartFormConfig};
//! use serde::Deserialize; //! use serde::Deserialize;
//! //!
//! #[derive(Debug, Deserialize)] //! #[derive(Debug, Deserialize)]
@@ -23,6 +23,7 @@
//! //!
//! #[derive(Debug, MultipartForm)] //! #[derive(Debug, MultipartForm)]
//! struct UploadForm { //! struct UploadForm {
//! // Note: the form is also subject to the global limits configured using `MultipartFormConfig`.
//! #[multipart(limit = "100MB")] //! #[multipart(limit = "100MB")]
//! file: TempFile, //! file: TempFile,
//! json: MpJson<Metadata>, //! json: MpJson<Metadata>,
@@ -38,10 +39,15 @@
//! //!
//! #[actix_web::main] //! #[actix_web::main]
//! async fn main() -> std::io::Result<()> { //! async fn main() -> std::io::Result<()> {
//! HttpServer::new(move || App::new().service(post_video)) //! HttpServer::new(move || {
//! .bind(("127.0.0.1", 8080))? //! App::new()
//! .run() //! .service(post_video)
//! .await //! // Also increase the global total limit to 100MiB.
//! .app_data(MultipartFormConfig::default().total_limit(100 * 1024 * 1024))
//! })
//! .bind(("127.0.0.1", 8080))?
//! .run()
//! .await
//! } //! }
//! ``` //! ```
//! //!