So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By PatrykW
#73645 Hello everyone.
I'm trying to get ESP8266 to download a bunch of files from a server and to save them to SPIFFS, but I'm having a hard time making it happen.
I can get a file from a server and load it to a string variable using this:
Code: Select allString url = "http://domain.com/file.whatever";
HTTPClient http;
http.begin(url);
String payload = http.getString();
http.end();

and then i can save the string to a file using something like that:
Code: Select allvoid stringToFile(String fileName, String text) {
  File f = SPIFFS.open(fileName, "w");
  f.write((uint8_t *)text.c_str(), text.length());
  f.close();
}

It works just fine with small text files, but it crashes with larger files.
Can someone help me with this one? ;)
User avatar
By Erol444
#78137 You should be saving data stream from server, not saving everything in RAM and then writing it to SPIFFS..
I got it working like this :)
Code: Select all    String url = String(UPDATE_URL) + response.substring(4);
    //url = http://123.123.123.123/SPIFFS/test2.htm
    String file_name=response.substring(response.lastIndexOf('/'));
    //file_name = test2.htm
    Serial.println(url);
    File f = SPIFFS.open(file_name, "w");
    if (f) {
      http.begin(url);
      int httpCode = http.GET();
      if (httpCode > 0) {
        if (httpCode == HTTP_CODE_OK) {
          http.writeToStream(&f);
        }
      } else {
        Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
      }
      f.close();
    }
    http.end();