Skip to content

Commit 310857a

Browse files
committed
Add exemple Upload Huge File
1 parent 172083f commit 310857a

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-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,129 @@
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 printDirectory() {
41+
if (!server.hasArg("dir")) {
42+
return returnFail("BAD ARGS");
43+
}
44+
String path = server.arg("dir");
45+
if (path != "/" && !SD.exists((char *)path.c_str())) {
46+
return returnFail("BAD PATH");
47+
}
48+
File dir = SD.open((char *)path.c_str());
49+
path = String();
50+
if (!dir.isDirectory()) {
51+
dir.close();
52+
return returnFail("NOT DIR");
53+
}
54+
dir.rewindDirectory();
55+
server.setContentLength(CONTENT_LENGTH_UNKNOWN);
56+
server.send(200, "text/json", "");
57+
WiFiClient client = server.client();
58+
59+
server.sendContent("[");
60+
for (int cnt = 0; true; ++cnt) {
61+
File entry = dir.openNextFile();
62+
if (!entry) {
63+
break;
64+
}
65+
66+
String output;
67+
if (cnt > 0) {
68+
output = ',';
69+
}
70+
71+
output += "{\"type\":\"";
72+
output += (entry.isDirectory()) ? "dir" : "file";
73+
output += "\",\"name\":\"";
74+
output += entry.path();
75+
output += "\"";
76+
output += "}";
77+
server.sendContent(output);
78+
entry.close();
79+
}
80+
server.sendContent("]");
81+
dir.close();
82+
}
83+
84+
void handleNotFound() {
85+
digitalWrite(led, 1);
86+
String message = "File Not Found\n\n";
87+
message += "URI: ";
88+
message += server.uri();
89+
message += "\nMethod: ";
90+
message += (server.method() == HTTP_GET) ? "GET" : "POST";
91+
message += "\nArguments: ";
92+
message += server.args();
93+
message += "\n";
94+
for (uint8_t i = 0; i < server.args(); i++) {
95+
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
96+
}
97+
server.send(404, "text/plain", message);
98+
digitalWrite(led, 0);
99+
}
100+
101+
void setup(void) {
102+
Serial.begin(115200);
103+
104+
while (!SD.begin()) delay();
105+
Serial.println("SD Card initialized.");
106+
107+
WiFi.mode(WIFI_STA);
108+
WiFi.begin(ssid, password);
109+
110+
while (WiFi.status() != WL_CONNECTED) {
111+
delay(500);
112+
Serial.print(".");
113+
}
114+
Serial.print("Connected to ");
115+
Serial.println(ssid);
116+
Serial.print("IP address: ");
117+
Serial.println(WiFi.localIP());
118+
119+
server.on(UriRegex("/upload/(.*)"), HTTP_PUT, handleCreate, handleCreateProcess);
120+
server.onNotFound(handleNotFound);
121+
server.begin();
122+
server.println("HTTP server started");
123+
124+
}
125+
126+
void loop(void) {
127+
server.handleClient();
128+
delay(2);//allow the cpu to switch to other tasks
129+
}

0 commit comments

Comments
 (0)