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