Skip to content

Commit a924ba1

Browse files
ficetoficeto
ficeto
authored and
ficeto
committed
add proper POST support and more methods
GET params are always added plain POST is added to the GET arguments Uploads are handled by separate handler
1 parent e6bb6b3 commit a924ba1

File tree

3 files changed

+350
-79
lines changed

3 files changed

+350
-79
lines changed

libraries/ESP8266WebServer/examples/SDWebServer/SDWebServer.ino

+33
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,26 @@ MDNSResponder mdns;
4343
ESP8266WebServer server(80);
4444

4545
static bool hasSD = false;
46+
File uploadFile;
47+
48+
void handleFileUpload(){
49+
if(server.uri() != "/upload") return;
50+
HTTPUpload upload = server.upload();
51+
if(upload.status == UPLOAD_FILE_START){
52+
Serial.print("Upload: START, filename:");
53+
Serial.println(upload.filename);
54+
if(SD.exists((char *)upload.filename.c_str())) SD.remove((char *)upload.filename.c_str());
55+
uploadFile = SD.open(upload.filename.c_str(), FILE_WRITE);
56+
} else if(upload.status == UPLOAD_FILE_WRITE){
57+
Serial.print("Upload: WRITE, Bytes:");
58+
Serial.println(upload.buflen);
59+
if(uploadFile) uploadFile.write(upload.buf, upload.buflen);
60+
} else if(upload.status == UPLOAD_FILE_END){
61+
Serial.print("Upload: END, Size:");
62+
Serial.println(upload.size);
63+
if(uploadFile) uploadFile.close();
64+
}
65+
}
4666

4767
bool loadFromSdCard(String path){
4868
String dataType = "text/plain";
@@ -152,6 +172,19 @@ void setup(void){
152172
//Attach handler
153173
server.onNotFound(tryLoadFromSdCard);
154174

175+
//Attach Upload handler
176+
server.onFileUpload(handleFileUpload);
177+
178+
//Attach handler for the Upload location
179+
server.on("/upload", HTTP_POST, [](){
180+
WiFiClient client = server.client();
181+
String message = "HTTP/1.1 200 OK\r\n";
182+
message += "Content-Type: text/plain\r\n";
183+
message += "Access-Control-Allow-Origin: *\r\n";
184+
message += "\r\n";
185+
client.print(message);
186+
});
187+
155188
//start server
156189
server.begin();
157190
Serial.println("HTTP server started");

0 commit comments

Comments
 (0)