views:

36

answers:

1

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

+1  A: 

This isn't really specific to Content-Disposition.

You can't change the HTTP status once you start sending the response body. If an error occurs at this point of time, the only method I'm aware of for signaling an error would be to close the connection.

Julian Reschke