1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-21 10:26:06 +03:00

"Fix" sketches and libs to use the new upload api

This commit is contained in:
Me No Dev 2015-11-19 23:20:03 +02:00
parent 3e4418ac3f
commit 0063d80c74
4 changed files with 14 additions and 21 deletions

View File

@ -36,12 +36,9 @@ void ESP8266HTTPUpdateServer::setup(ESP8266WebServer *server)
_server->sendHeader("Access-Control-Allow-Origin", "*"); _server->sendHeader("Access-Control-Allow-Origin", "*");
_server->send(200, "text/plain", (Update.hasError())?"FAIL":"OK"); _server->send(200, "text/plain", (Update.hasError())?"FAIL":"OK");
ESP.restart(); ESP.restart();
}); },[&](){
// handler for the file upload, get's the sketch bytes, and writes
// handler for the file upload, get's the sketch bytes, and writes // them through the Update object
// them through the Update object.
_server->onFileUpload([&](){
if(_server->uri() != "/update") return;
HTTPUpload& upload = _server->upload(); HTTPUpload& upload = _server->upload();
if(upload.status == UPLOAD_FILE_START){ if(upload.status == UPLOAD_FILE_START){
if (_serial_output) if (_serial_output)
@ -70,6 +67,6 @@ void ESP8266HTTPUpdateServer::setup(ESP8266WebServer *server)
Update.end(); Update.end();
if (_serial_output) Serial.println("Update was aborted"); if (_serial_output) Serial.println("Update was aborted");
} }
yield(); delay(0);
}); });
} }

View File

@ -207,10 +207,9 @@ void setup(void){
server.on("/edit", HTTP_PUT, handleFileCreate); server.on("/edit", HTTP_PUT, handleFileCreate);
//delete file //delete file
server.on("/edit", HTTP_DELETE, handleFileDelete); server.on("/edit", HTTP_DELETE, handleFileDelete);
//called after file upload //first callback is called after the request has ended with all parsed arguments
server.on("/edit", HTTP_POST, [](){ server.send(200, "text/plain", ""); }); //second callback handles file uploads at that location
//called when a file is received inside POST data server.on("/edit", HTTP_POST, [](){ server.send(200, "text/plain", ""); }, handleFileUpload);
server.onFileUpload(handleFileUpload);
//called when the url is not defined here //called when the url is not defined here
//use it to load content from SPIFFS //use it to load content from SPIFFS

View File

@ -256,9 +256,8 @@ void setup(void){
server.on("/list", HTTP_GET, printDirectory); server.on("/list", HTTP_GET, printDirectory);
server.on("/edit", HTTP_DELETE, handleDelete); server.on("/edit", HTTP_DELETE, handleDelete);
server.on("/edit", HTTP_PUT, handleCreate); server.on("/edit", HTTP_PUT, handleCreate);
server.on("/edit", HTTP_POST, [](){ returnOK(); }); server.on("/edit", HTTP_POST, [](){ returnOK(); }, handleFileUpload);
server.onNotFound(handleNotFound); server.onNotFound(handleNotFound);
server.onFileUpload(handleFileUpload);
server.begin(); server.begin();
DBG_OUTPUT_PORT.println("HTTP server started"); DBG_OUTPUT_PORT.println("HTTP server started");

View File

@ -27,8 +27,12 @@ void setup(void){
server.sendHeader("Access-Control-Allow-Origin", "*"); server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(200, "text/html", serverIndex); server.send(200, "text/html", serverIndex);
}); });
server.onFileUpload([](){ server.on("/update", HTTP_POST, [](){
if(server.uri() != "/update") return; server.sendHeader("Connection", "close");
server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(200, "text/plain", (Update.hasError())?"FAIL":"OK");
ESP.restart();
},[](){
HTTPUpload& upload = server.upload(); HTTPUpload& upload = server.upload();
if(upload.status == UPLOAD_FILE_START){ if(upload.status == UPLOAD_FILE_START){
Serial.setDebugOutput(true); Serial.setDebugOutput(true);
@ -52,12 +56,6 @@ void setup(void){
} }
yield(); yield();
}); });
server.on("/update", HTTP_POST, [](){
server.sendHeader("Connection", "close");
server.sendHeader("Access-Control-Allow-Origin", "*");
server.send(200, "text/plain", (Update.hasError())?"FAIL":"OK");
ESP.restart();
});
server.begin(); server.begin();
MDNS.addService("http", "tcp", 80); MDNS.addService("http", "tcp", 80);