Ethernet HTML send file (almost!)

This seems so simple, but I’m having a really hard time figuring it out.
Multiple symptoms & questions in a single piece of code.

this exact code worked last night - zero other changes that I’m aware of.
The incoming text file was around 150KB, and I could transfer and open the file in my browser - no problem.
But if I tried to transfer from ‘half-way-through’ the file it only sent an empty file!
webClient() is open and connected outside this code - and is valid (since the transfer header seems to get through ok…)

Today with smaller files <1KB - nothing at all, yet the SD.read is functioning as expected.

If I check the file read() - the chars are all coming in as expected, and reaching the write() function. Just not getting to the client??! Tell me it’s something obvious!

I should add that the browser’s download indicator showed progress when the files /were/ transferring, but just appear and wait for me to click them with the empty files…

tl;dr; Worked with an entire large file, but not smaller, or partial files.
Today not at all… ?!!

THE CODE:
//==================================================
void send_file() {
//==================================================
char c, tempBuffer[32];
int rCounter;
File srcFile;
webClient.println(“HTTP/1.1 200 OK”);
webClient.println(“Content-Type: text/plain”);
webClient.println(“Content-disposition: attachment; filename=theFile.txt”);
webClient.println(“Connection: close”);

srcFile = SD.open(“incoming.txt”, FILE_READ);
if (srcFile) {
srcFile.seek(0L);
while (srcFile.available()) {
// read file chunks into a buffer
char *rPtr = tempBuffer; // pointer to start of buffer
for (rCounter=0; rCounter < sizeof(tempBuffer); rCounter++) {
c = srcFile.read();
if (c < 0) break; // end of file - fall through to write()
*rPtr++ = c;
}
// send packet of rCounter bytes to webclient
webClient.write(tempBuffer, rCounter);
}
logFile.close();
}
webClient.stop();
}
//==================================================

I suggest that you'll get better help at http://snippets-r-us.com.

PaulS is right. Post all your code.

But from what I see, you have at least one error. You need to put a blank line between the header and the body. Add “\r\n” to the last header line.

webClient.println("Connection: close\r\n");

Thanks. All the code is about 2500 lines. So I'll hold on to it...!

I can't believe it that the CR/LF solved the immediate problem.
Thanks so much for spotting that!

I still have some minor bugs, but with a lot of data flying around ad sharing char arrays - it is getting closer!

Hex is currently 75K, and about 14K of RAM in use. (ATMEGA 1284P) won't grow much more. All the features and functions are running.

THANKS AGAIN for spotting that.

Thanks again to SurferTim - that was the last major hurdle (!)
All other niggling bugs seem to have been ironed out - and the project is 'bangin' ! (Language way out of my league!)

Just to summarise what the project does...

  • Serves a (single) serial and/or telnet user session,

  • a web page with POST updates of CLI & web managed variables.

  • SD card logging of events, changes and status notifications (time stamped from NTP)

  • Conditional relay operation on states & bitmask filters.

  • Load/Save settings to SD.

  • HTML/Web download of log files.

  • and the actual background task of monitoring LAN connected equipment !

And it all runs in 75K code / 14K RAM / 188 bytes of EEPROM (on a 1284P)
I'll try to optimize it down, but there are a lot of strings laying around.
RAM is mainly used for (local vars obviously), plus HTML buffering and building friendly text output messages.

Sorry - I'm not going to post 2793 lines of my code (plus libraries extra).. I'm thinking about making this a product for sale! If you have any KS or marketing skill - feel free to get in touch! (Australia)