user16684: 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(&root, &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) && (count < BUFSIZE) && (webFile.available()))
    {
        char c = webFile.read();
        if (0 > 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';

}


user11763: 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 > c)
    done = true;

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

16684: <p>I have a server with files that may be downloaded.</p>

<p>How do I go about detecting when a download is cancelled by a client?</p>

<p>The function that handles reading files for download:</p>

<pre><code>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';

}
</code></pre>
 11763: <p>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:</p>

<pre><code>bool dload_cancel = false;
</code></pre>

<p>Change the <code>IF</code> block, below the innermost <code>WHILE</code> loop, to this:</p>

<pre><code>if (count){
  if (client.connected())
    client.write( buffers, count);
  else {
    dloadCancel = true;
    break;
  }
}
</code></pre>

<p>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 <code>DO-WHILE</code> block to know if the download was completed or not. </p>

<pre><code>if (dloadCancel){
  // client was disconnected; download did not complete
}
</code></pre>

<p><code>dloadCancel</code> is also a better indicator of a completed download than your <code>fileDownload</code> flag.</p>

<p>You should also know that this:</p>

<pre><code>if (0 &gt; c)
    done = true;
</code></pre>

<p>is redundant. Its already taken care of by <code>webFile.available()</code>.</p>
