Skip to content

Commit 2dd14e9

Browse files
committed
Add exemple Upload Huge File
1 parent de75193 commit 2dd14e9

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Upload Huge File To SD Over Http
2+
3+
This project is an example of an HTTP server designed to facilitate the transfer of large files using the PUT method, in accordance with RFC specifications.
4+
5+
### Example cURL Command
6+
7+
```bash
8+
curl -X PUT -H "Content-Type: application/octet-stream" -T ./my-file.mp3 http://esp-ip/upload/my-file.mp3
9+
```
10+
11+
## Resources
12+
13+
- RFC HTTP/1.0 - Additional Request Methods - PUT : [Link](https://datatracker.ietf.org/doc/html/rfc1945#appendix-D.1.1)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
#include <WiFi.h>
2+
#include <WiFiClient.h>
3+
#include <WebServer.h>
4+
#include <uri/UriRegex.h>
5+
#include <SD.h>
6+
7+
const char* ssid = "**********";
8+
const char* password = "**********";
9+
10+
WebServer server(80);
11+
12+
File rawFile;
13+
void handleCreate() {
14+
server.send(200, "text/plain", "");
15+
}
16+
void handleCreateProcess() {
17+
String path = server.pathArg(0);
18+
HTTPRaw& raw = server.raw();
19+
if (raw.status == RAW_START) {
20+
if (SD.exists((char *)path.c_str())) {
21+
SD.remove((char *)path.c_str());
22+
}
23+
rawFile = SD.open(path.c_str(), FILE_WRITE);
24+
Serial.print("Upload: START, filename: ");
25+
Serial.println(path);
26+
} else if (raw.status == RAW_WRITE) {
27+
if (rawFile) {
28+
rawFile.write(raw.buf, raw.currentSize);
29+
}
30+
Serial.print("Upload: WRITE, Bytes: ");
31+
Serial.println(raw.currentSize);
32+
} else if (raw.status == RAW_END) {
33+
if (rawFile) {
34+
rawFile.close();
35+
}
36+
Serial.print("Upload: END, Size: ");
37+
Serial.println(raw.totalSize);
38+
}
39+
}
40+
41+
void returnFail(String msg) {
42+
server.send(500, "text/plain", msg + "\r\n");
43+
}
44+
45+
void printDirectory() {
46+
if (!server.hasArg("dir")) {
47+
return returnFail("BAD ARGS");
48+
}
49+
String path = server.arg("dir");
50+
if (path != "/" && !SD.exists((char *)path.c_str())) {
51+
return returnFail("BAD PATH");
52+
}
53+
File dir = SD.open((char *)path.c_str());
54+
path = String();
55+
if (!dir.isDirectory()) {
56+
dir.close();
57+
return returnFail("NOT DIR");
58+
}
59+
dir.rewindDirectory();
60+
server.setContentLength(CONTENT_LENGTH_UNKNOWN);
61+
server.send(200, "text/json", "");
62+
WiFiClient client = server.client();
63+
64+
server.sendContent("[");
65+
for (int cnt = 0; true; ++cnt) {
66+
File entry = dir.openNextFile();
67+
if (!entry) {
68+
break;
69+
}
70+
71+
String output;
72+
if (cnt > 0) {
73+
output = ',';
74+
}
75+
76+
output += "{\"type\":\"";
77+
output += (entry.isDirectory()) ? "dir" : "file";
78+
output += "\",\"name\":\"";
79+
output += entry.path();
80+
output += "\"";
81+
output += "}";
82+
server.sendContent(output);
83+
entry.close();
84+
}
85+
server.sendContent("]");
86+
dir.close();
87+
}
88+
89+
void handleNotFound() {
90+
String message = "File Not Found\n\n";
91+
message += "URI: ";
92+
message += server.uri();
93+
message += "\nMethod: ";
94+
message += (server.method() == HTTP_GET) ? "GET" : "POST";
95+
message += "\nArguments: ";
96+
message += server.args();
97+
message += "\n";
98+
for (uint8_t i = 0; i < server.args(); i++) {
99+
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
100+
}
101+
server.send(404, "text/plain", message);
102+
}
103+
104+
void setup(void) {
105+
Serial.begin(115200);
106+
107+
while (!SD.begin()) delay(1);
108+
Serial.println("SD Card initialized.");
109+
110+
WiFi.mode(WIFI_STA);
111+
WiFi.begin(ssid, password);
112+
113+
while (WiFi.status() != WL_CONNECTED) {
114+
delay(500);
115+
Serial.print(".");
116+
}
117+
Serial.print("Connected to ");
118+
Serial.println(ssid);
119+
Serial.print("IP address: ");
120+
Serial.println(WiFi.localIP());
121+
122+
server.on(UriRegex("/upload/(.*)"), HTTP_PUT, handleCreate, handleCreateProcess);
123+
server.onNotFound(handleNotFound);
124+
server.begin();
125+
Serial.println("HTTP server started");
126+
127+
}
128+
129+
void loop(void) {
130+
server.handleClient();
131+
delay(2);//allow the cpu to switch to other tasks
132+
}

0 commit comments

Comments
 (0)