-->
Page 1 of 1

Need help with downloading files and saving them to SPIFFS

PostPosted: Fri Feb 02, 2018 4:45 am
by PatrykW
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? ;)

Re: Need help with downloading files and saving them to SPIF

PostPosted: Fri Sep 07, 2018 10:54 am
by Erol444
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();