Page 1 of 1

SPIFFS file read failure in webserver

Posted: Wed Oct 02, 2019 6:42 pm
by techno500
Code is from a "GET" request that lists SPIFFS files as download able links; read by readFile function. first pass downlowloaded file is good; all attempts fail, after first download:

Code: Select all

               // Check the action to see if it was a GET request.
                         else if (strcmp(path, "/SdBrowse") == 0)   // Respond with the path that was accessed.
                         {

                              // send a standard http response header
                              client.println("HTTP/1.1 200 OK");
                              client.println("Content-Type: text/html");
                              client.println();
                              client.println("<!DOCTYPE HTML>");
                              client.println("<html>");
                              client.println("<head><title>SDBrowse</title>");
                              // print all the files, use a helper to keep it clean
                              client.println("<h2>Collected Observations</head></h2>");
                              client.println("<body><h3><br>");

                             
                              String str = String("<!DOCTYPE HTML><html><head></head>");

                              if (!SPIFFS.begin(true))
                              {
                                   Serial.println("An Error has occurred while mounting SPIFFS");
                                   return;
                              }

                              File root = SPIFFS.open("/");

                              File file = root.openNextFile();

                              while (file)
                              {

                                   if(strncmp(file.name(), "/LOG", 4) == 0)
                                   {
                                        str += "<a href=\"";
                                        str += file.name();
                                        str += "\">";
                                        str += file.name();
                                        str += "</a>";
                                        str += "    ";
                                        str += file.size();
                                        str += "<br>\r\n";                                       
                                   }

                                   file = root.openNextFile();
                              }

                              client.print(str);

                             
                              client.println("</h3><br><br>");
                              client.println("<a href=http://" + publicIP + ":" +  LISTEN_PORT + "/Weather    >Home</a><br>");
                              client.println("</body>");
                              client.println("</html>");

                              end();

                         }
                         // Check the action to see if it was a GET request.
Problem occurs when "SdBrowse" web page remains open and a after 1st download, a second file is selected. Second file does not download. I have inserted numeric values to check Sketch flow; it appears to complete properly, just no file download.

Serial monitor output showing Sketch flow in readFile function:

Client connected: Wed , 10/02/2019 , 01:26:45 EDT
Client IP: 10.0.0.146
Processing request
Action: GET
Path: /SdBrowse
Client closed: Wed , 10/02/2019 , 01:26:46 EDT

Client connected: Wed , 10/02/2019 , 01:26:48 EDT
Client IP: 10.0.0.146
Processing request
Action: GET
Path: /LOG02102019.TXT
File55: /LOG02102019.TXT
57
59
60
61
Client closed: Wed , 10/02/2019 , 01:26:48 EDT

Client connected: Wed , 10/02/2019 , 01:26:56 EDT
Client IP: 10.0.0.146
Processing request
Action: GET
Path: /LOG01102019.TXT
File55: /LOG01102019.TXT
57
59
60
61
60
61
60
61
60
61
60
61
60
61
60
61
60
61
60
61
60
61
60
61
60
61
60
61
60
61
60
61
60
61
60
61
Client closed: Wed , 10/02/2019 , 01:26:56 EDT

Function readFile:

Code: Select all

void readFile()
{

     if(!SPIFFS.begin(true))
     {
        Serial.println("An Error has occurred while mounting SPIFFS");
        return;
     }
     
     //     digitalWrite(online, HIGH);   //turn-on online LED indicator

     String filename = (const char *)&MyBuffer;

     Serial.print("File55:  ");
     Serial.println(filename);


     File webFile = SPIFFS.open(filename);

     Serial.println("57");

     if (!webFile)
     {
          Serial.println("File58 LOG file failed to open");
          Serial.println("\n");
     }
     else
     {
         
          Serial.println("59");
         
          char buf[1024];
          int len;
          while  (len = webFile.read((uint8_t *)buf, 1024))
          {

               Serial.println("60");

               
               
               client.write((const char*)buf, len);

               Serial.println("61");
          }

         

          webFile.flush();

          webFile.close();

     }

     error = 0;

     delayTime = 1000;

     MyBuffer[0] = '\0';

//     digitalWrite(online, LOW);   //turn-off online LED indicator

}
How do I enable downloading of second file; while web page remains open. Why is this issue happening? How do I fix code?

ESP32 Web site for project: http://bit.ly/2lcu0NX

William