Chat freely about anything...

User avatar
By Sirquil
#63258 Have been trying to convert this Arduino SD "listFile" function; have not been successful:

Code: Select allchar ListFiles(Adafruit_CC3000_ClientRef client, uint8_t flags, SdFile dir)
{
     // This code is just copied from SdFile.cpp in the SDFat library
     // and tweaked to print to the client output in html!
     dir_t p;

     dir.rewind();
     client.println("<ul>");

     while (dir.readDir(&p) > 0)
     {
          // done if past last used entry
          if (p.name[0] == DIR_NAME_FREE) break;

          // skip deleted entry and entries for . and  ..
          if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.') continue;

          // only list subdirectories and files
          if (!DIR_IS_FILE_OR_SUBDIR(&p)) continue;

          // print any indent spaces
          client.print("<li><a href=\"");
          for (uint8_t i = 0; i < 11; i++)
          {
               if (p.name[i] == ' ') continue;
               if (i == 8)
               {
                    client.print('.');
               }
               client.print((char)p.name[i]);
          }
          if (DIR_IS_SUBDIR(&p))
          {
               client.print('/');
          }
          client.print("\">");

          // print file name with possible blank fill
          for (uint8_t i = 0; i < 11; i++)
          {
               if (p.name[i] == ' ') continue;
               if (i == 8)
               {
                    client.print('.');
               }
               client.print((char)p.name[i]);
          }

          if (DIR_IS_SUBDIR(&p))
          {
               client.print('/');
          }
          client.print("</a>");



          // print modify date/time if requested
          if (flags & LS_DATE)
          {
               dir.printFatDate(p.lastWriteDate);
               client.print(' ');
               dir.printFatTime(p.lastWriteTime);
          }
          // print size if requested
          if (!DIR_IS_SUBDIR(&p) && (flags & LS_SIZE))
          {
               client.print(' ');
               client.print(p.fileSize);
          }
          client.println("</li>");
     }
     client.println("</ul>");
}


This is what listFiles function looks like in client browser:

Image

Help in moving listFiles function to the SPIFFS of the ESP8266, please.

All help, most appreciated.
William
Last edited by Sirquil on Sat Mar 11, 2017 4:01 am, edited 1 time in total.
User avatar
By Sirquil
#63274 Able to list files; no pointer linked to filename:

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.
{

     fileDownload = 1;

     // 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>\r\n");
     client.println("<body>\r\n");
     client.println("<head><title>SDBrowse</title><head />");
     // print all the files, use a helper to keep it clean
     client.println("<h2>Server Files:</h2>");
     //ListFiles(client, LS_SIZE, root);
     
     client.print("<ul>");
     Dir dir = SPIFFS.openDir("/");
     while (dir.next())
     {
     String fileName = dir.fileName();
     client.print("<li><a href:/fileName");
     client.print("</ a></ li></ ul>");
     size_t fileSize = dir.fileSize();
     client.printf("%s, size: %s\n", fileName.c_str(), formatBytes(fileSize).c_str());
     client.println("<br />\r\n");
     } client.printf("\n");
     
     client.println("<br />\r\n");
     client.println("<br />\r\n");
     
     client.println("");
     client.println("");     
     client.println("<a href=http://10.0.0.41:8001/Weather    >Current Observations</a><br />");
     client.println("<br />\r\n");
     client.println("<body />\r\n");
     client.println("<br />\r\n");
     client.println("</html>\r\n");

     fileDownload = 0;

}


Compiles and lists content of root directory.

<a href:/"" is not working; continuing work coding. Suggestions welcome.
William
User avatar
By martinayotte
#63297 You don't need the above code to get a list of files from SPIFFS.
Simply use openDir() function that SPIFFS provides :
Code: Select all  if (!SPIFFS.begin()) {
    Serial.println("SPIFFS failed to mount !\r\n");
  }
  Dir dir = SPIFFS.openDir("/");
  str = "";
  while (dir.next()) {
    str += dir.fileName();
    str += " - ";
    str += dir.fileSize();
    str += "\r\n";
  }
  Serial.println(str);
User avatar
By Sirquil
#63306 Thank you for your message; I have a finished Arduino project I would like to use with the WeMOS D1 R2 (ESP8266Ex.) My project uses the ListFiles function in my first post in this message thread. I need to find a way to use a pointer to hold filename when url link is selected. Pointer content is used to open the filename selected in the read mode; in the fileRead function.

Statements like "dir_t p;" I need to find a way to implement in SPIFFS. Other statements; "dir.readDir(&p," "p.name[0],and "uint8_t" is where I am requesting help.

@martinayotte I did use your method prior to trying the one in my second post in this message thread. I have been Googling trying to find more information about ESP8266 file system; beyond basic command. One of the examples from the Arduino IDE, ESP8266Webserver, FSWebServer is where I found the method used in my 2nd post. I am having great difficulty reading the FS.cpp and FS.h library files and finding useful information; although I believe it is contained in the library files.

Temporary webpage for my Arduino project: http://tinyurl.com/zataxoo Project is open Source and available: https://github.com/Tech500/SdWebBrowse-CC3000-HTTPServer

William