ESP32 SPIFFS reading file into String

hacker007
Posts: 4
Joined: Sat Mar 17, 2018 10:24 pm

ESP32 SPIFFS reading file into String

Postby hacker007 » 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:

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();
}
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.

Code: Select all

  File gif = SPIFFS.open("/config.json", "r");
  if (gif) {
    HTTP.send(200, "text/plain", gif.readString());
    gif.close();
  }
Config.json is 258 bytes long, about.html is 2975 bytes long.

Any idea?

hacker007
Posts: 4
Joined: Sat Mar 17, 2018 10:24 pm

Re: ESP32 SPIFFS reading file into String

Postby hacker007 » Fri Mar 23, 2018 7:07 am

Yes. I figured out what the problem is. It is the current ESP-arduino release. People seem to have problems with SPIFFS from december up to now (end of march 2018) and still. The problem is, it uses old MKSPIFFS.exe. A fix for now is: If you update it manually to latest repease 0.2.3, SPIFFS will work. See: https://github.com/igrr/mkspiffs/releases. It gets installed in hardware/espressif/esp32/tools/mkspiffs.

Hope I helped anyone.

realoldguy
Posts: 3
Joined: Sun Apr 18, 2021 6:22 pm

Re: ESP32 SPIFFS reading file into String

Postby realoldguy » Sat Jan 08, 2022 8:37 am

Yes - I am having the exact same problem. Not solved yet. I opened the extensions in VS code and checked all updates were in place.
Not very helpfull, but if anyone could explain to me what I need to update I would be very gratefull :D :oops:

Who is online

Users browsing this forum: No registered users and 94 guests