Web browser recursive file download function

Hi guys, I am trying to create a function that downloads file by file all the files in the sd card by clicking a button in a web browser. At the moment I've got this:

if(download == 1){    //If we have clicked the button
          
           SD.vwd()->rewind();
           while (file.openNext(SD.vwd(), O_WRITE)) {
           
                      
           client.println(F("HTTP/1.1 200 OK"));
           client.println(F("Content-Type: application/octet-stream"));   //application/zip
   //      client.println(F("Content-Disposition: attachment; filename = \"2016.zip\""));
           client.println(F("Content-Disposition: attachment; file.name()"));
           client.println(F("Connnection: close"));
           client.println();

           Serial.println(F("downloading..."));

            char buffer[ BUFSIZE ];
            int i;

            while ( file.available() ) 
            {
                           
               for (i=0 ; (i < BUFSIZE) && (buffer[ i-1 ] >= 0) ; i++)
                 {
                   buffer[i] = file.read();

                  }
               client.write( buffer, (buffer[i-1] >= 0) ? i : i-1 );

             }
             exit;  
            }

              // reset buffer index and all buffer elements to 0
                index = 0;
                StrClear(clientline, BUFSIZ);
                file.close();
                Serial.println(F("All files downloaded!"));
         
        }
         }

But it does not work, it shows in the serial monitor as if it was downloading it but nothing happens, so if someone could possibly help me to figure out how to correct this code it would be great.

Thanks :slight_smile: .

Firstly, if you expect to get meaningfull answers please, post all your code, the error can, and normally is, outside the posted snippet.

Saying that "It does not work" doesn't help much too. Post you Serial Monitor outputs with some debug prints.

On a quick look at your code, it seens fine, except for the O_WRITE, when you open it to write it open already at the end of the file, that may cause what you are seeing, but I'm not sure, and I doubt anyone can help further with just this snippet.