And then later in code I realize that an error ocurred and I need to cancel the download process and instead output the data as HTML (in a normal way). Is there any way to do that?
Once the page has finished processing, the response output is sent to the client. That is the point of no return. Before that point, you can alter the headers that you set by calling the
ClearHeaders method, and the
Clear method if you want to remove any output that might have been buffered.
Create a javascript function that checks every few seconds if there was an error after the users clicks the download button, if there's an error you can then change the page HTML with another javascript function.
If you call Response.End or Response.Redirect, the page will terminate processing at that point. Or if you call Response.TransmitFile or Response.BinaryWrite, for example, you cannot then stop the download.
You see, Mike, if I call Response.End() after calling the methods from my first post I get an empty ZIP attachment in a client browser. This obviously will be confusing for a user. What I'm trying to do is to "cancel" the donwload
process and display a web page with an error description instead.
dc2000
Member
68 Points
104 Posts
Cancel started file download with ASP.NET
Mar 28, 2012 06:42 AM|LINK
Say, if I started file downloading process by outputting headers like so:
Response.ContentType = "application/zip"; Response.AppendHeader("content-disposition", "attachment; filename=\"" + strZipName + "\""); Response.CacheControl = "Private"; Response.Cache.SetExpires(DateTime.Now.AddMinutes(3));And then later in code I realize that an error ocurred and I need to cancel the download process and instead output the data as HTML (in a normal way). Is there any way to do that?
Mikesdotnett...
All-Star
155649 Points
19987 Posts
Moderator
MVP
Re: Cancel started file download with ASP.NET
Mar 29, 2012 07:16 AM|LINK
Once the page has finished processing, the response output is sent to the client. That is the point of no return. Before that point, you can alter the headers that you set by calling the ClearHeaders method, and the Clear method if you want to remove any output that might have been buffered.
Web Pages CMS | My Site | Twitter
dc2000
Member
68 Points
104 Posts
Re: Cancel started file download with ASP.NET
Mar 29, 2012 07:20 AM|LINK
Thanks. But at what point does the "page finish processing"?
OniShiro
Member
99 Points
20 Posts
Re: Cancel started file download with ASP.NET
Mar 29, 2012 08:30 AM|LINK
You can do it in a convoluted way.
Create a javascript function that checks every few seconds if there was an error after the users clicks the download button, if there's an error you can then change the page HTML with another javascript function.
Mikesdotnett...
All-Star
155649 Points
19987 Posts
Moderator
MVP
Re: Cancel started file download with ASP.NET
Mar 29, 2012 01:17 PM|LINK
If you call Response.End or Response.Redirect, the page will terminate processing at that point. Or if you call Response.TransmitFile or Response.BinaryWrite, for example, you cannot then stop the download.
Web Pages CMS | My Site | Twitter
dc2000
Member
68 Points
104 Posts
Re: Cancel started file download with ASP.NET
Mar 29, 2012 08:17 PM|LINK
You see, Mike, if I call Response.End() after calling the methods from my first post I get an empty ZIP attachment in a client browser. This obviously will be confusing for a user. What I'm trying to do is to "cancel" the donwload process and display a web page with an error description instead.