ESP32 SPIFFS reading file into String
Posted: Sat Mar 17, 2018 10:38 pm
Hi. I want to read a text .html file from SPIFFS into a String variable and use .replace commands in Arduino. Afterwards I plan to send them to the HTTP client. It works great on esp8266...
For now I tried:
I get the "- read from file:". What works and what doesn't is written above.
On ESP8266 I used: indexHTML = file.readString(); and it worked perfectly. Now it doesn't...
The code below works great... but I want to edit the text before the send and use: HTTP.send (200, "text/html", indexHTML); in the end.
Config.json is 258 bytes long, about.html is 2975 bytes long.
Any idea?
For now I tried:
Code: Select all
//I call it with: readFile(SPIFFS, "/about.html");
//The file exists!!!
void readFile(fs::FS &fs, const char * path){
String indexHTML;
unsigned int cnt = 0;
DBG_PORT.printf("Reading file: %s\r\n", path);
File file = fs.open(path);
if(!file || file.isDirectory()){
DBG_PORT.println("- failed to open file for reading");
return;
}
DBG_PORT.print("- read from file:");
while(file.available()) {
//indexHTML = file.readString(); //<= DOES NOT WORK!!!
//HTTP.write(file.read()); //<= DOES NOT WORK!!!
Serial.write(file.read()); //<= DISPLAYS UNLIMITED "?" until it crashes
//indexHTML += file.read(); //<= DOES NOT WORK!!!
cnt++;
}
DBG_PORT.println(cnt);
//HTTP.send (200, "text/html", indexHTML);
//THE CODE BELOW WORKS
//size_t sent = HTTP.streamFile(file, "text/html");
//DBG_PORT.print("Sent:");
//DBG_PORT.println(sent);
file.close();
}
On ESP8266 I used: indexHTML = file.readString(); and it worked perfectly. Now it doesn't...
The code below works great... but I want to edit the text before the send and use: HTTP.send (200, "text/html", indexHTML); in the end.
Code: Select all
File gif = SPIFFS.open("/config.json", "r");
if (gif) {
HTTP.send(200, "text/plain", gif.readString());
gif.close();
}
Any idea?