How to handle error with content-disposition

Posted by František Žiacik on Stack Overflow See other posts from Stack Overflow or by František Žiacik
Published on 2010-05-17T22:22:01Z Indexed on 2010/05/18 10:00 UTC
Read the original article Hit count: 266

Hi,

how should I handle an exception that occurs after sending a Content-Disposition header for an attachment? I'm trying to generate a report at server and send it as a file, but if an exception occurs during the report generation, the error message itself is sent to browser which still takes it as a content of a file and shows a Save As dialog. User cannot know there was an error generating report, saves the file which is in wrong format now.

Is there a way to cancel the response with this header and redirect to an error page? Or what else can I do to inform user about the error?

Probably I could generate the report first and only if there was no error send the headers, but I want the report render directly to the Response output stream so that it does not need to stay in memory.

Here is my code:

this.Response.ContentType = "application/octet-stream";
this.Response.AddHeader("Content-Disposition", @"attachment; filename=""" + item.Name + @"""");
this.Response.Flush();

GenerateReportTo(this.Response.OutputStream); // Exception occurs

Thanks for any suggestions

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET