forked from me-no-dev/ESPAsyncWebServer
-
Notifications
You must be signed in to change notification settings - Fork 88
Fix multipart file upload handling and improve error responses #329
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| // SPDX-License-Identifier: LGPL-3.0-or-later | ||
| // Copyright 2016-2025 Hristo Gochkov, Mathieu Carbou, Emil Muratov | ||
|
|
||
| // | ||
| // Demo to upload a firmware and filesystem image via multipart form data | ||
| // | ||
|
|
||
| #include <Arduino.h> | ||
| #if defined(ESP32) || defined(LIBRETINY) | ||
| #include <AsyncTCP.h> | ||
| #include <WiFi.h> | ||
| #elif defined(ESP8266) | ||
| #include <ESP8266WiFi.h> | ||
| #include <ESPAsyncTCP.h> | ||
| #elif defined(TARGET_RP2040) || defined(TARGET_RP2350) || defined(PICO_RP2040) || defined(PICO_RP2350) | ||
| #include <RPAsyncTCP.h> | ||
| #include <WiFi.h> | ||
| #endif | ||
|
|
||
| #include <ESPAsyncWebServer.h> | ||
| #include <StreamString.h> | ||
| #include <LittleFS.h> | ||
|
|
||
| // ESP32 example ONLY | ||
| #ifdef ESP32 | ||
| #include <Update.h> | ||
| #endif | ||
|
|
||
| static AsyncWebServer server(80); | ||
|
|
||
| void setup() { | ||
| Serial.begin(115200); | ||
|
|
||
| if (!LittleFS.begin()) { | ||
| LittleFS.format(); | ||
| LittleFS.begin(); | ||
| } | ||
|
|
||
| #if SOC_WIFI_SUPPORTED || CONFIG_ESP_WIFI_REMOTE_ENABLED || LT_ARD_HAS_WIFI || CONFIG_ESP32_WIFI_ENABLED | ||
| WiFi.mode(WIFI_AP); | ||
| WiFi.softAP("esp-captive"); | ||
| #endif | ||
|
|
||
| // ESP32 example ONLY | ||
| #ifdef ESP32 | ||
|
|
||
| // Shows how to get the fw and fs (names) and filenames from a multipart upload, | ||
| // and also how to handle multiple file uploads in a single request. | ||
| // | ||
| // This example also shows how to pass and handle different parameters having the same name in query string, post form and content-disposition. | ||
| // | ||
| // Execute in the terminal, in order: | ||
| // | ||
| // 1. Build firmware: pio run -e arduino-3 | ||
| // 2. Build FS image: pio run -e arduino-3 -t buildfs | ||
| // 3. Flash both at the same time: curl -v -F "name=Bob" -F "fw=@.pio/build/arduino-3/firmware.bin" -F "fs=@.pio/build/arduino-3/littlefs.bin" http://192.168.4.1/flash?name=Bill | ||
| // | ||
| server.on( | ||
| "/flash", HTTP_POST, | ||
| [](AsyncWebServerRequest *request) { | ||
| if (request->getResponse()) { | ||
| // response already created | ||
| return; | ||
| } | ||
|
|
||
| // list all parameters | ||
| Serial.println("Request parameters:"); | ||
| const size_t params = request->params(); | ||
| for (size_t i = 0; i < params; i++) { | ||
| const AsyncWebParameter *p = request->getParam(i); | ||
| Serial.printf("Param[%u]: %s=%s, isPost=%d, isFile=%d, size=%u\n", i, p->name().c_str(), p->value().c_str(), p->isPost(), p->isFile(), p->size()); | ||
| } | ||
|
|
||
| Serial.println("Flash / Filesystem upload completed"); | ||
|
|
||
| request->send(200, "text/plain", "Upload complete"); | ||
| }, | ||
| [](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) { | ||
| Serial.printf("Upload[%s]: index=%u, len=%u, final=%d\n", filename.c_str(), index, len, final); | ||
|
|
||
| if (request->getResponse() != nullptr) { | ||
| // upload aborted | ||
| return; | ||
| } | ||
|
|
||
| // start a new content-disposition upload | ||
| if (!index) { | ||
| // list all parameters | ||
| const size_t params = request->params(); | ||
| for (size_t i = 0; i < params; i++) { | ||
| const AsyncWebParameter *p = request->getParam(i); | ||
| Serial.printf("Param[%u]: %s=%s, isPost=%d, isFile=%d, size=%u\n", i, p->name().c_str(), p->value().c_str(), p->isPost(), p->isFile(), p->size()); | ||
| } | ||
|
|
||
| // get the content-disposition parameter | ||
| const AsyncWebParameter *p = request->getParam(asyncsrv::T_name, true, true); | ||
| if (p == nullptr) { | ||
| request->send(400, "text/plain", "Missing content-disposition 'name' parameter"); | ||
| return; | ||
| } | ||
|
|
||
| // determine upload type based on the parameter name | ||
| if (p->value() == "fs") { | ||
| Serial.printf("Filesystem image upload for file: %s\n", filename.c_str()); | ||
| if (!Update.begin(UPDATE_SIZE_UNKNOWN, U_SPIFFS)) { | ||
| Update.printError(Serial); | ||
| request->send(400, "text/plain", "Update begin failed"); | ||
| return; | ||
| } | ||
|
|
||
| } else if (p->value() == "fw") { | ||
| Serial.printf("Firmware image upload for file: %s\n", filename.c_str()); | ||
| if (!Update.begin(UPDATE_SIZE_UNKNOWN, U_FLASH)) { | ||
| Update.printError(Serial); | ||
| request->send(400, "text/plain", "Update begin failed"); | ||
| return; | ||
| } | ||
|
|
||
| } else { | ||
| Serial.printf("Unknown upload type for file: %s\n", filename.c_str()); | ||
| request->send(400, "text/plain", "Unknown upload type"); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| // some bytes to write ? | ||
| if (len) { | ||
| if (Update.write(data, len) != len) { | ||
| Update.printError(Serial); | ||
| Update.end(); | ||
| request->send(400, "text/plain", "Update write failed"); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| // finish the content-disposition upload | ||
| if (final) { | ||
| if (!Update.end(true)) { | ||
| Update.printError(Serial); | ||
| request->send(400, "text/plain", "Update end failed"); | ||
| return; | ||
| } | ||
|
|
||
| // success response is created in the final request handler when all uploads are completed | ||
| Serial.printf("Upload success of file %s\n", filename.c_str()); | ||
| } | ||
| } | ||
| ); | ||
|
|
||
| #endif | ||
|
|
||
| server.begin(); | ||
| } | ||
|
|
||
| // not needed | ||
| void loop() { | ||
| delay(100); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies for my ignorance - is there a reason to require both name and filename before presenting either? Would it be better to treat them independently?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They always come together: when creating a file upload in a form we pass the name and the file through html tags or JavaScript.
see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Content-Disposition#as_a_header_for_a_multipart_body
If not in a form multipart, then only the filename is known and it is passed always on the callback as done already.
This pr exposes the content disposition parameters in the case of a form upload and this is especially required when the form upload contains several files inside (multiple uploads in a single request) because the name parameter is the only way to distinguish these file uploads.