简体   繁体   English

如何处理内容处置错误

[英]How to handle error with content-disposition

how should I handle an exception that occurs after sending a Content-Disposition header for an attachment? 发送附件的Content-Disposition标头后,应如何处理发生的异常? 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. 可能我可以先生成报告,并且只有在没有错误的情况下才发送标题,但是我希望报告直接呈现到Response输出流,这样它就不必保留在内存中。

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 感谢您的任何建议

This isn't really specific to Content-Disposition. 这并不是真正针对内容处置。

You can't change the HTTP status once you start sending the response body. 一旦开始发送响应正文,就无法更改HTTP状态。 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. 如果此时发生错误,那么我知道的唯一一种发出错误信号的方法就是关闭连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM