File download with Arduino web server?

Hi,

I'm new on Arduino.
First, sorry for my english.
I would like to dowload a .txt file stored in the SD Card by a local network.

I use Arduino UNO, Ethernet Shield with Wiznet W5500.
my .txt file called TEST.txt

With all good informations found on this forum, i did the following code,
but when i put the IP address in my web browser, I have an automatic dowload of a file called "TEST.txt" but it is totally empty!
I don't understand why I haven't my data in my file.

Thank you for your help

#include <SD.h>
#include <SPI.h>
#include <Ethernet2.h>

#define REQ_BUF_SZ   20
byte mac[] = {0x90, 0xA2, 0xDA, 0x10, 0xA5, 0xC0};
IPAddress ip(169, 254, 155, 166);
byte gateway []={169, 254, 155, 1};
byte subnet []={255, 255, 0, 0};
EthernetServer server(80);
File myFile;
char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP request stored as null terminated string
char req_index = 0;
String readString; 

//////////////////////

void setup()
{
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);
  Serial.begin(9600);     
  // initialize SD card
  Serial.println("Initializing SD card...");
  if (!SD.begin(4)) 
  {
     Serial.println("ERROR - SD card initialization failed!");
     return; 
  }
  Serial.println("SUCCESS - SD card initialized.");
  if (!SD.exists("TEST.txt")) 
  {
     Serial.println("ERROR - Can't find index.htm file!");
     return;  // can't find index file
  }
  Serial.println("SUCCESS - Found TEST.txt file.");
  Ethernet.begin(mac, ip, gateway, subnet);  // initialize Ethernet device
  server.begin();           // start to listen for clients
  Serial.println("Ready");
}

void loop()
{
  // Create a client connection
  EthernetClient client = server.available();
  if (client) 
  {
     boolean currentLineIsBlank = true;
     while (client.connected()) 
     {
        if (client.available()) 
        {
           char c = client.read();
           //read char by char HTTP request
           if (readString.length() < 100) 
           {
              //store characters to string 
              readString += c; 
              Serial.print(c);
           } 
           //if HTTP request has ended
           if (c == '\n' && currentLineIsBlank) 
           {
              // open requested web page file
              client.println();
              if (HTTP_req, "GET /TEST.txt") 
              {
                 client.println("HTTP/1.1 200 OK"); //send new page
                 client.println("Content-Disposition: attachment; filename=TEST.txt");
                 client.println("Connection: close");
                 client.println();      
                 Serial.println(readString); //print to serial monitor for debuging 
                 if (myFile) 
                 {
                    byte clientBuf[64];
                    int clientCount = 0;
                    while(myFile.available())
                    {
                       clientBuf[clientCount] = myFile.read();
                       clientCount++;
                       if(clientCount > 63)
                       {
                       client.write(clientBuf,64);
                       clientCount = 0;
                       }
                    }
                    //final <64 byte cleanup packet
                    if(clientCount > 0) client.write(clientBuf,clientCount);            
                    // close the file:
                    myFile.close();
                 }
                 delay(1);
                 //stopping client
                 client.stop();
                 readString="";
              }
           }
        }
     } 
  }
}

why did you open a second thread?

try Content-Length header and longer delay (100ms) before closing

Do you know for sure the TEST.txt file is not empty on SD card ?