How to detect cancelled download? I have a server with files that may be downloaded.

How do I go about detecting when a download is cancelled by a client?

The function that handles reading files for download:

void readFile()
{

EthernetClient client = server.available(); 

// Open file for Reading.
SdFile webFile;
webFile.open(&amp;root, &amp;MyBuffer[1], O_READ);   
if (!webFile.isOpen()) error("readFile");

do   // @ adafruit_support_rick's do-while loop
{
    int count = 0;
    char buffers[BUFSIZE];
    bool done = false;

    while ((!done) &amp;&amp; (count &lt; BUFSIZE) &amp;&amp; (webFile.available()))
    {
        char c = webFile.read();
        if (0 &gt; c)
        done = true;
        else
        buffers[count++] = c;
        delayMicroseconds(1000);
    }

    if (count)
    client.write( buffers, count);

} while (webFile.available());

webFile.close();

fileDownload = 0;  //File download has finished; allow logging since download has completed

delay(500);

MyBuffer[0] = '\0';

}
Well, assuming cancelling a download == incomplete download == client disconnected before server was done, you could create a flag like this at the top of the function:

bool dload_cancel = false;


Change the IF block, below the innermost WHILE loop, to this:

if (count){
  if (client.connected())
    client.write( buffers, count);
  else {
    dloadCancel = true;
    break;
  }
}


The flag will be set once your server checks and discovers that the client has been disconnected. You can check the state of the flag outside the DO-WHILE block to know if the download was completed or not. 

if (dloadCancel){
  // client was disconnected; download did not complete
}


dloadCancel is also a better indicator of a completed download than your fileDownload flag.

You should also know that this:

if (0 &gt; c)
    done = true;


is redundant. Its already taken care of by webFile.available().