pearcewg
pearcewg

Reputation: 9613

ASP.NET file download - detect if user cancelled download?

I have an ASP.NET application which lets users download files from my web server. I track the downloads, and only allow so many downloads of a file.

I have the following code to invoke the download for my users (on click of a button):

string sURL = sExternalSequenceFullPath + Session["sFilename"].ToString();
FileInfo fileInfo = new FileInfo(sURL);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileInfo.Name);
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.Flush();
Response.WriteFile(fileInfo.FullName);

My problem is that the browser presents the user with the ability to download (save/saveas) or cancel.

Is there a way to know if the user cancelled? If they did cancel, I don't want to count the download against them.

If not, is there another way to do with [without adding something to the client's machine].

Upvotes: 2

Views: 3362

Answers (5)

Michael
Michael

Reputation: 12011

How about this:

Response.BufferOutput = false;
Response.TransmitFile(fileInfo.FullName);
//increment download count

If you disable response output buffering then it won't move past the line of code that sends the file to the client until the client has finished receiving it, so you can safely increment the download count in the next line of code. As SamV said in his answer if they cancel the download even half way through it throws a HttpException so the increment download count code doesn't get run.

I also suggest using the TransmitFile method instead of the WriteFile method since it doesn't load the entire file into memory wasting server resources.

Upvotes: 0

SamV
SamV

Reputation: 194

Not sure it is still helpful, but I have pretty much the same code for managing downloads, except mine is wrapped in a try..catch block, due to being in a referenced assembly for my site and the need to throw the exception up to my global error handler.

when a user cancels the download, i get a 'The remote host closed the connection.' exception and i step into my catch block.

I may be way off the mark (I have only been coding for a year :) ) but perhaps it could be something as simple as decrementing the download count in a catch block?

Upvotes: 2

Hanlet Escaño
Hanlet Escaño

Reputation: 17370

you can use Sockets and download the file using the HTTP protocol (or FTP but would still need to access by using UserName and Password). If you do utilize Sockets though, the user will not be shown the Browser's download window. This might make users distrust your site. I don't think it is possible to achieve by using the Browser's download Dialog, I will keep checking on this thread however to see if it's possible at all. Check this out to find some Socket programming examples. Good luck on this task.

Hanlet

EDIT: I just found This check it out, maybe it might help you.

Upvotes: 0

rick schott
rick schott

Reputation: 20617

There is no way that I know of this can be done. I would present the user with a dialog, that would call your download code. If they selected "Yes" to the dialog, I would count that against them.

The other option is to write a plugin(Silverlight, Flash...etc) so that you have control.

Upvotes: 3

Dennis Traub
Dennis Traub

Reputation: 51634

Have you tried to handle the cancel button's click event?

Upvotes: 0

Related Questions