Skip to content

Commit d1ac845

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

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 <SD.h>
5+
6+
const char* ssid = "**********";
7+
const char* password = "**********";
8+
9+
WebServer server(80);
10+
11+
File rawFile;
12+
void handleCreate() {
13+
server.send(200, "text/plain", "");
14+
}
15+
void handleCreateProcess() {
16+
String path = server.pathArg(0);
17+
HTTPRaw& raw = server.raw();
18+
if (raw.status == RAW_START) {
19+
if (SD.exists((char *)path.c_str())) {
20+
SD.remove((char *)path.c_str());
21+
}
22+
rawFile = SD.open(path.c_str(), FILE_WRITE);
23+
Serial.print("Upload: START, filename: ");
24+
Serial.println(path);
25+
} else if (raw.status == RAW_WRITE) {
26+
if (rawFile) {
27+
rawFile.write(raw.buf, raw.currentSize);
28+
}
29+
Serial.print("Upload: WRITE, Bytes: ");
30+
Serial.println(raw.currentSize);
31+
} else if (raw.status == RAW_END) {
32+
if (rawFile) {
33+
rawFile.close();
34+
}
35+
Serial.print("Upload: END, Size: ");
36+
Serial.println(raw.totalSize);
37+
}
38+
}
39+
40+
void returnFail(String msg) {
41+
server.send(500, "text/plain", msg + "\r\n");
42+
}
43+
44+
void printDirectory() {
45+
if (!server.hasArg("dir")) {
46+
return returnFail("BAD ARGS");
47+
}
48+
String path = server.arg("dir");
49+
if (path != "/" && !SD.exists((char *)path.c_str())) {
50+
return returnFail("BAD PATH");
51+
}
52+
File dir = SD.open((char *)path.c_str());
53+
path = String();
54+
if (!dir.isDirectory()) {
55+
dir.close();
56+
return returnFail("NOT DIR");
57+
}
58+
dir.rewindDirectory();
59+
server.setContentLength(CONTENT_LENGTH_UNKNOWN);
60+
server.send(200, "text/json", "");
61+
WiFiClient client = server.client();
62+
63+
server.sendContent("[");
64+
for (int cnt = 0; true; ++cnt) {
65+
File entry = dir.openNextFile();
66+
if (!entry) {
67+
break;
68+
}
69+
70+
String output;
71+
if (cnt > 0) {
72+
output = ',';
73+
}
74+
75+
output += "{\"type\":\"";
76+
output += (entry.isDirectory()) ? "dir" : "file";
77+
output += "\",\"name\":\"";
78+
output += entry.path();
79+
output += "\"";
80+
output += "}";
81+
server.sendContent(output);
82+
entry.close();
83+
}
84+
server.sendContent("]");
85+
dir.close();
86+
}
87+
88+
void handleNotFound() {
89+
String message = "File Not Found\n\n";
90+
message += "URI: ";
91+
message += server.uri();
92+
message += "\nMethod: ";
93+
message += (server.method() == HTTP_GET) ? "GET" : "POST";
94+
message += "\nArguments: ";
95+
message += server.args();
96+
message += "\n";
97+
for (uint8_t i = 0; i < server.args(); i++) {
98+
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
99+
}
100+
server.send(404, "text/plain", message);
101+
digitalWrite(led, 0);
102+
}
103+
104+
void setup(void) {
105+
Serial.begin(115200);
106+
107+
while (!SD.begin()) delay();
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+
server.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)