views:

371

answers:

3

I have the following code that allows a user to download a file. I need to know (if possible) if they downloaded the file successfully. Is there any kind of callback that I can tie into to know whether or not they were successful in download it? Thanks.

string filename = Path.GetFileName(url);
context.Response.Buffer = true;
context.Response.Charset = "";
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.ContentType = "application/x-rar-compressed";
context.Response.AddHeader("content-disposition", "attachment;filename=" + filename);
context.Response.TransmitFile(context.Server.MapPath(url));
context.Response.Flush();
+4  A: 

Why not add one more line that lets you know it's finished? After the context.Response.Flush(), it should be done.

Dean J
I guess that was my question. I didn't know if it continued synchronously after the transmitfile call or if it just called that, let it do it's thing and then called flush. I'll give that a try. Thanks.
geoff
A: 

I guess this is not possible.

Response is just a memory object which interacts with IIS. You cannot know whether the browser completely downloaded a file, since the user might cancel just before the last byte arrived, but after IIS finished sending the whole stream.

You might try to implement an IHttpHandler, continuously write chunks of the file to context.Response in the Process() method, and Flush() and check like this

context.Response.Flush();
if (!context.Response.IsClientConnected)
// handle disconnect

That's the closest thing I can think of solving your problem.

devio
+1  A: 
Rubens Farias