How to delete a file from the SD card via a web page on PC

Hello, I have an Arduino mega + shield w5100 sketch running a beehive monitoring program.
I have currently implemented a server function which allows me to download from my pc (mega ip address) the files located on the micro SD of the Ethernet shield.

I am not the one who designed this program (thanks to the many examples) I am new to programming ...

I would like to find some help (tutorial or others ...) to add to this function of downloading files from the SD to the PC the option of remotely deleting files (in addition to being able to download them).

Has anyone seen an example of this or could help me design it?

Thank you

PS: I am attaching the code for the file download server part.

/ * File listing function * /
void ListFiles(EthernetClient client, uint8_t flags, File dir) {    
  client.println("<ul>");
  while (true) {
    File entry = dir.openNextFile();
   
    // here is what we do if there is no more entry in the directory
     if (! entry) {
       // No more file
       break;     // we exit the while
                   }
 
   // print all the indent spaces
    client.print("<li><a href=\"");
    client.print(entry.name());
    if (entry.isDirectory()) {
       client.println("/");
                             }
    client.print("\">");
    
  // print the name of the file with a possible filling of empty spaces
    client.print(entry.name());
    if (entry.isDirectory()) {
       client.println("/");
                             }
        
    client.print("</a>");
 
    client.println("</li>");
    entry.close();
  }   // end of while
  client.println("</ul>");
                                                          }   // end of the function lists the files
 
// How big should our row buffer be. 100 is already a lot!
//#define BUFSIZ 100



/ * Function of creation of the listing of the files present on the SD * /

void printDirectory(File dir, int numTabs) {      
   while(true) {
     File entry =  dir.openNextFile();
     if (! entry) {
       // No more file
       break;
     }
     for (uint8_t i=0; i<numTabs; i++) {
       Serial.print('\t');
     }
     Serial.print(entry.name());
     if (entry.isDirectory()) {
       Serial.println("/");
       printDirectory(entry, numTabs+1);
     } else {
      // files have sizes, directories don't have
       Serial.print("\t\t");
       Serial.println(entry.size(), DEC);
     }
     entry.close();
   }
}         //end of printDirectory




/ * Remote access function SD files on Arduino (web server) * /

void afficheserveur(){          // display web page
  
char clientline[BUFSIZ];
char name[17];
int index = 0;

  
  EthernetClient client = server.available();
  if (client) {
    root = SD.open("/");
    printDirectory(root, 0);      // go to the function for creating the listing of the files present on the SD
    
    // initialization with one end of the HTTP request with an empty line
    boolean current_line_is_blank = true;
    
    // reset the input buffer
    index = 0;
    
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        
        // If it is not a new line, add the character to the buffer
        if (c != '\n' && c != '\r') {                    // get a \ n or \ r newline, means the string is complete
          clientline[index] = c;
          index++;
          // Are we too big for the buffer? If this is the case we start to throw away data
          if (index >= BUFSIZ) 
            index = BUFSIZ -1;
          
          // we keep reading more data!
          continue;
                                     }  // end of if
        
        // get a \ n or \ r newline, means the string is complete
        clientline[index] = 0;
        
        // Display for debugging
        Serial.println(clientline);
        
        // Find a substring such as a request to get the file
        if (strstr(clientline, "GET /") != 0) 
        {
          // this time no space after the /, so a subfile!
          char *filename;
          
          filename = clientline + 5; // a little trick, look after the "GET /" (5 characters) *******
          // find the string "HTTP / 1.1" and change the first character of the substring to a 0 to clear it.
          (strstr(clientline, " HTTP"))[0] = 0;
 
          if(filename[strlen(filename)-1] == '/') {  // Split the name of the directory file
            filename[strlen(filename)-1] = 0;        // because the opening generates an error if we leave the character / at the end
                                                  } // end of if filename
          
          Serial.print(F("Web query for : ")); 
          Serial.println(filename);  // Display the name of the requested file
 
          File file = SD.open(filename, O_READ);
          if ( file == 0 ) {  // Opening the file with the return code of 0 is an error in SDFile.open
            client.println("HTTP/1.1 404 Not Found");
            client.println("Content-Type: text/html");
            client.println();
            client.println("<h2>File Not Found!</h2>");
            client.println("
<h3>Couldn't open the File!</h3>");
            break; 
                          }   // end of if file open error
          
          Serial.println("File open!");
                    
          client.println("HTTP/1.1 200 OK");
          if (file.isDirectory()) {
            Serial.println(F("It's a directory"));
            //file.close();
            client.println("Content-Type: text/html");
            client.println();
            client.print("<h2>Fichiers dans /");
            client.print(filename); 
            client.println(":</h2>");
            ListFiles(client,LS_SIZE,file);  
            file.close();                   
                                 } 
          else { // Any non-directory clicked, the server will send the file to the client for download
            client.println("Content-Type: application/octet-stream");       
            client.println();
          
            char file_buffer[16];
            int avail;
            while (avail = file.available()) {
              int to_read = min(avail, 16);
              if (to_read != file.read(file_buffer, to_read)) {
                break;
                                                              }
              // uncomment the Serial.write to debug (it's slow!)
              //Serial.write((char)c);
              client.write(file_buffer, to_read);
                                             }// end of while dispo
            file.close();
               }    // end of else
               
        }   // end of strstr clientline 
        
        else {
          // Any error is a 404 error
          client.println("HTTP/1.1 404 Not Found");
          client.println("Content-Type: text/html");
          client.println();
          client.println("<h2>File Not Found!</h2>");
            } //end of else
        break;
                               } // end of if client available
                               } //end of u while connected
    // give the web browser time to receive the data
    delay(100);
    client.stop();
  } // end of if client
}                                // end of the Arduino web server function

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.