Skip to content

add proper POST support and more methods
add proper POST support and more methods
 #203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from May 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,26 @@ MDNSResponder mdns;
ESP8266WebServer server(80);

static bool hasSD = false;
File uploadFile;

void handleFileUpload(){
if(server.uri() != "/upload") return;
HTTPUpload upload = server.upload();
if(upload.status == UPLOAD_FILE_START){
Serial.print("Upload: START, filename:");
Serial.println(upload.filename);
if(SD.exists((char *)upload.filename.c_str())) SD.remove((char *)upload.filename.c_str());
uploadFile = SD.open(upload.filename.c_str(), FILE_WRITE);
} else if(upload.status == UPLOAD_FILE_WRITE){
Serial.print("Upload: WRITE, Bytes:");
Serial.println(upload.buflen);
if(uploadFile) uploadFile.write(upload.buf, upload.buflen);
} else if(upload.status == UPLOAD_FILE_END){
Serial.print("Upload: END, Size:");
Serial.println(upload.size);
if(uploadFile) uploadFile.close();
}
}

bool loadFromSdCard(String path){
String dataType = "text/plain";
Expand Down Expand Up @@ -152,6 +172,19 @@ void setup(void){
//Attach handler
server.onNotFound(tryLoadFromSdCard);

//Attach Upload handler
server.onFileUpload(handleFileUpload);

//Attach handler for the Upload location
server.on("/upload", HTTP_POST, [](){
WiFiClient client = server.client();
String message = "HTTP/1.1 200 OK\r\n";
message += "Content-Type: text/plain\r\n";
message += "Access-Control-Allow-Origin: *\r\n";
message += "\r\n";
client.print(message);
});

//start server
server.begin();
Serial.println("HTTP server started");
Expand Down
Loading