bool
handleFileRead(
String
path) {
// send the right file to the client (if it exists)
Serial
.
println
(
"handleFileRead: "
+
path);
if
(path
.
endsWith
(
"/"
)) path
+=
"index.html"
;
// If a folder is requested, send the index file
String
contentType
=
getContentType(path);
// Get the MIME type
String
pathWithGz
=
path
+
".gz"
;
if
(SPIFFS
.
exists
(pathWithGz)
||
SPIFFS
.
exists
(path)) {
// If the file exists, either as a compressed archive, or normal
if
(SPIFFS
.
exists
(pathWithGz))
// If there's a compressed version available
path
+=
".gz"
;
// Use the compressed verion
File
file
=
SPIFFS
.
open
(path
,
"r"
);
// Open the file
size_t
sent
=
server
.
streamFile(file
,
contentType);
// Send it to the client
file
.
close
();
// Close the file again
Serial
.
println
(
String
(
"\tSent file: "
)
+
path);
return
true
;
}
Serial
.
println
(
String
(
"\tFile Not Found: "
)
+
path);
// If the file doesn't exist, return false
return
false
;
}
void
handleFileUpload(){
// upload a new file to the SPIFFS
HTTPUpload
&
upload
=
server
.
upload();
if
(upload
.
status
==
UPLOAD_FILE_START){
String
filename
=
upload
.
filename;
if
(
!
filename
.
startsWith
(
"/"
)) filename
=
"/"
+
filename;
Serial
.
(
"handleFileUpload Name: "
);
Serial
.
println
(filename);
fsUploadFile
=
SPIFFS
.
open
(filename
,
"w"
);
// Open the file for writing in SPIFFS (create if it doesn't exist)
filename
=
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
.
(
"handleFileUpload Size: "
);
Serial
.
println
(upload
.
totalSize);
server
.
sendHeader(
"Location"
,
"/success.html"
);
// Redirect the client to the success page
server
.
send
(303);
}
else
{
server
.
send
(500
,
"text/plain"
,
"500: couldn't create file"
);
}
}
}
The
handleFileUpload
function just writes the file attached to the POST request to SPIFFS.
If you wan to use other file types as well, you can just add them to the
getContentType
function.
Uploading files
To upload a new file to the ESP, or to update an existing file, just go to http://esp8266.local/upload, click the Choose File button, select
the file you wish to upload, and click Upload. You can now enter the URL into the URL bar, and open the new file.
A note on safety
This example isn't very secure (obviously). Everyone that can connect to the ESP can upload new files, or edit the existing files and
insert XSS code, for example. There's also not a lot of error checking/handling, like checking if there's enough space in the SPIFFS to
upload a new file, etc.
Advanced example
The code for these SPIFFS server examples comes (for the most part) from an example written by Hristo Gochkov. You can find it under
File > Examples > ESP8266WebServer > FSBrowser. It has a web interface for browsing and editing files in your browser, and has
some other nice features as well.