How to check Response.BinaryWrite success

J

jhansl

Hello,

I am trying desperately how to find out if the bytes sent in a
Response.BinaryWrite (or bytes written to Response.OutputStream) are
ACTUALLY sent to the client. My problem is that I have files I would
like to send to client only one time, so I need to check to see that
all the bytes were actually sent to the client. As far as I can tell,
there seems to be no way of checking to see if the bytes were sent. If
I run a test and cancel the download mid-way through the transfer, the
BinaryWrite() method just returns without throwing any kind of
exception. There has to be a way to find out if the operation was
cancelled, timed out, or was incomplete. Here is my code:

private const int BUFFER_SIZE = 32768; // 32K buffer

public void ProcessRequest(HttpContext context)
{
HttpRequest Request = context.Request;
HttpResponse Response = context.Response;

try
{
string sourcePath =
ConfigurationManager.AppSettings["MasterSourcePath"];

string filename = Path.Combine(sourcePath,
"1001/1003/5000151.wma");

Response.Buffer = false;
Response.BufferOutput = false;
Response.ContentType = "audio/x-msdownload";
Response.AddHeader("Content-Disposition",
"attachment;filename=Test.wma");
Stream outstream = Response.OutputStream;
FileStream instream = new FileStream(filename,
FileMode.Open, FileAccess.Read, FileShare.Read);

byte[] buffer = new byte[BUFFER_SIZE];
int len;
while ((len = instream.Read(buffer, 0, BUFFER_SIZE)) > 0)
{
outstream.Write(buffer, 0, len);
}
outstream.Flush();

instream.Close();
}
catch (Exception ex)
{
Response.Write("Could not serve clip: " + ex.Message);
}
}

Thank You,
Josh
 
B

Bruce Barker

you are correct, there is no way to know from the server side if a browser
completed a download successfully. there can be a lot of boxes between the
server and client (sometimes the proxy will close the server connection
before sending the data to the client). the http protocol does no completion
notification.

the most common approach is a client side application, that notifies the
server if it was successful download.

-- bruce (sqlwork.com)
 
J

jhansl

Thank you for your reply Bruce.

I fully expected that there was no completion status reporting in the
HTTP protocol. What I was hoping for, however, was a way to at least
get the information on the server side if it successfully sent out all
the bytes. That's really all I need to know for this particular
implementation. The interesting thing is that I experimented with PHP
on an Apache web server... and when I actually write the bytes out to
the client, the script stops dead in its tracks if the connection is
broken at any time. I know this is most likely due to the fact that PHP
is scripting, while .NET is treated as a full application on the
server. I just wish there was a way to get that kind of behavior in
..NET.

Regards,
Josh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top