The server is an ESP8266 micro-controller, which writes to internal
flash memory. I suspect the write times would be sluggish.
My initial thoughts were that the writes were not completed by the
time the webpage change occurred. As larger files would be uploaded
in multiple chunks, I tried files of only a few bytes and had the
same problem.
The password security is to prevent dumb users accidentally messing
up the system. It's not imperative.
webServer.on("/files.html", HTTP_POST, []() { webServer.send(200, "text/plain", "");}, handleFileUpload);
void handleFileUpload(){
Serial.printf("Line %4d inside handleFileUpload\n",__LINE__); // upload a new file to the LittleFS
HTTPUpload& upload = webServer.upload();
String path;
if(upload.status == UPLOAD_FILE_START){
path = upload.filename;
if(!path.startsWith("/")) path = "/"+path;
if(!path.endsWith(".gz")) { // The file server always prefers a compressed version of a file
String pathWithGz = path+".gz"; // So if an uploaded file is not compressed, the existing compressed
if(LittleFS.exists(pathWithGz)) // version of that file must be deleted (if it exists)
LittleFS.remove(pathWithGz);
}
Serial.print("handleFileUpload Name: "); Serial.println(path);
fsUploadFile = LittleFS.open(path, "w"); // Open the file for writing in LittleFS (create if it doesn't exist)
path = String();
} else if(upload.status == UPLOAD_FILE_WRITE){
if(fsUploadFile)
fsUploadFile.write(upload.buf, upload.currentSize); // Write the received bytes to the file
} else if(upload.status == UPLOAD_FILE_END){
if(fsUploadFile) { // If the file was successfully created
fsUploadFile.close(); // Close the file again
Serial.print("handleFileUpload Size: ");
Serial.println(upload.totalSize);
} else {
webServer.send(500, "text/plain", "500: couldn't create file");
}
}
}