Downloading a folder from web browser

Hi guys, I'm trying to download a folder (in zip format) in my SD card using a webserver i have created. The problem is that when I click the .html of the folder it downloads a file named download.zip. When I try to open this .zip an error message comes up: "The file has an unkown format or it's damaged", but with my .CSV files it downloads and opens correctly with the correct name. Here is the specific code i use for the download:

if(file.isDir())
{
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: application/zip");
client.println("Content-Disposition: attachment;");
client.println("Connnection: close");
client.println();

Serial.println(F("Downloading folder"));

char buffer[ BUFSIZE ];
int i;

if ( file.available() )
{

for (i=0 ; (i < BUFSIZE) && (buffer[ i-1 ] >= 0) ; i++)
{
buffer = 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();*
    NOTE: file has been previously opened
    Thanks for any help!

a zip is a file, not a directory not sure what you are trying to do

where is that code supposed to execute?

what is file type?

(use code tags to post code please)

A couple things I see right away.

First, you are only checking file.available once. That shouldn't be an "if", it should be a "while".

// change this
            if ( file.available() ) 
// to this
            while ( file.available() )

Second, you are not closing the connection. I see no client.stop() call.

My server code handles zip ok.
http://playground.arduino.cc/Code/WebServerST

J-M-L:
a zip is a file, not a directory not sure what you are trying to do

where is that code supposed to execute?

what is file type?

(use code tags to post code please)

Hi J-M-L , this code is supposed to execute after :

  if (strstr(clientline, "GET /") != 0) {  //clicked in a sub-file of root

the web browser looks like the images I have attached.

I can download .csv, .txt files, but I don't know how to download a complete folder for example : 2016/ that contains another folder (07/) and/or .csv files.

web_browser.png

web_browser2.png

SurferTim:
A couple things I see right away.

First, you are only checking file.available once. That shouldn't be an "if", it should be a "while".

// change this

if ( file.available() )
// to this
            while ( file.available() )




Second, you are not closing the connection. I see no client.stop() call.

My server code handles zip ok.
http://playground.arduino.cc/Code/WebServerST

Hi SurferTim, the code you are referring to plus the original I posted would be:

 if( file.isDir())  
          {
           client.println("HTTP/1.1 200 OK");
           client.println("Content-Type: application/zip");
           client.println("Connnection: close");
           client.println();

           Serial.println(F("Downloading folder..."));

            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();
                
                       
        } else {
          // everything else is a 404
          client.println("HTTP/1.1 404 Not Found");
          client.println("Content-Type: text/html");
          client.println();
          client.println("<h2>File Not Found!</h2>");
        }
        break;
      }
    }
    
    delay(1);
    client.stop();
  }
 }
}

NOTE: Any additional "{}" are because I have got more functions inside the loop

The question I have is if I can change the HTTP request so that it can download a full folder containing multiples .csv files (for example). Anyway I will take a look at your code, thank you.