0

Got me stumped... Using NodeMCU with SD ... classic WebServer setup working a simple File Server over HTTP: Here is the SendFile handler:

  void handleFileRequest()
{
  
  Serial.println("handleFileRequest");  
  File32 file = sd.open(fileName, O_READ);
      if ( file == 0 )  // Opening the file with return code of 0 is an error in SDFile.open
      { 
        handleError(404,"File Not Found");
        return; 
      }
      
      int fsizeDisk = file.size();
      if (file.isDirectory()|| fsizeDisk <=0)
      {
         handleError(500,"cannot send folder only file");
         return; 
      }
      
      Serial.print("file size: "); Serial.println(fsizeDisk);
      
      ledState = LOW;
      digitalWrite(LED_BUILTIN, ledState);
    
      unsigned long timeBegin = micros();
       
      server.sendHeader("Content-Length", (String)(fsizeDisk));
      server.sendHeader("Content-disposition", "attachment; filename=\"" + fileName + "\"");
      server.sendHeader("Cache-Control", "max-age=0, no-store"); // do not allow cache
      server.sendHeader("Connection", "close");
      size_t sent = server.streamFile(file, "application/octet-stream");
      unsigned long timeEnd = micros();
      unsigned long duration = timeEnd - timeBegin;
      double averageDuration = (double)duration / 1000.0;
      Serial.println("Duration: "); 
      Serial.print(averageDuration); Serial.println("s");
      server.client().stop();
      ledState = HIGH;
      digitalWrite(LED_BUILTIN, ledState); 
      file.close();
      delay(200);
      Serial.print("Data Sent: ");
      Serial.println(sent);
      delay(200);
}
And here is the Serial Terminal log...
Connected to hc406-ng
IP address: 192.168.0.162
MDNS responder started @GrnAcres-Hi
SdFat version: 2.0.6
Test with GrnAcres-Hi.local/download?file=test.jpg
Or with 192.168.0.162/download?file=test.jpg
HTTP server started
 
handleFileRequest
Request for: Hello.txt
file size: 13
Duration: 
1002.78s
Data Sent: 7
handleFileRequest
Request for: Hello.txt
file size: 13
Duration: 
1002.78s
Data Sent: 7
handleFileRequest
Request for: Hello.txt
file size: 13
Duration: 
1002.76s
Data Sent: 7
handleFileRequest
Request for: Hello.txt
file size: 13
Duration: 
1002.73s
Data Sent: 7
handleFileRequest
Request for: Hello.txt
file size: 13
Duration: 
1002.78s
Data Sent: 7
handleFileRequest
Request for: Hello.txt
file size: 13
Duration: 
1002.83s
Data Sent: 7

No matter which file I use... (test.jpg is 15K)... I get 50% of the file transmitted and then the handler is called again... and again... and again?

PLEASE NOTE: the duration in seconds is OFF... this is only a debugging value it takes only 1 second per loop. I simply wanted a ratio between each sucessive call

Any comments would help greatly... thanking you all in advance. Cheers

Bob Logan
  • 36
  • 2
  • PS: That client.stop is not suppose to be required when using content-length... just trying for effect.... – Bob Logan Jun 18 '21 at 20:40
  • Are you describing a multipart behavior? And if so, where is the problem? Check here: https://stackoverflow.com/questions/16958448/what-is-http-multipart-request – Nino Jun 19 '21 at 07:21
  • No... this is server side code... simple "application/octet-stream" MIME type as server response to client request for a file... Problem is that server only processes half of requested file... and then proceeds to loop over and over with that same request... sending only half of file each time through it's loop. There is no looping in the code... it is as if some 'interrupt' is aborting the transfer... and allowing the request to be reprocessed? Very wierd – Bob Logan Jun 19 '21 at 09:57
  • See if this helps: https://github.com/esp8266/Arduino/issues/3966#issuecomment-507624388 – Nino Jun 19 '21 at 10:19
  • Thank you for the link... but this is already incorporated in my code... See first server.sendHeader... which we understand must be first header... Content-Length controls when client will know to disconnect. We set cache so transfer is always occur when requested)... This link uses 'text/plain' MIME which we need binary as shown in my code. But nice try... – Bob Logan Jun 19 '21 at 21:39

0 Answers0