C# 正确使用 Asp.Net Response.TransmitFile 和 Response.End()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14566781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Correct usage of Asp.Net Response.TransmitFile and Response.End()
提问by Markus
What is the correct usage of this code?
这段代码的正确用法是什么?
httpContext.Response.AddHeader("Content-Disposition", "inline; filename=" + HttpUtility.UrlPathEncode(fileName));
httpContext.Response.ContentType = "image/png";
httpContext.Response.AddHeader("Content-Length", new FileInfo(physicalFileName).Length.ToString());
httpContext.Response.TransmitFile(physicalFileName);
httpContext.Response.Flush();
httpContext.Response.End(); //Use it or not?
Is it really good to use .Flush()
and .End()
?
用.Flush()
and真的好吗.End()
?
According to thisyou should never ever use Response.End()
(only in error or hacking scenarios)
根据这个你永远不应该使用Response.End()
(仅在错误或黑客场景中)
But in some of the answers the .End()
is reconmended...?
但是在某些答案中,.End()
建议使用...?
Like in thisarticle.
就像在这篇文章中一样。
So it is appropriate to use Response.End
or not?
那么到底用Response.End
还是不用合适呢?
回答by Marcus Vinicius
According to Thomas Marquardt, you should never use Response.End()
. Instead you should use Context.ApplicationInstance.CompleteRequest()
. Check this articleas well, it's from Microsoft KB, recommending the use of Application.CompleteRequest()
instead Response.End()
.
根据Thomas Marquardt 的说法,您永远不应该使用Response.End()
. 相反,您应该使用Context.ApplicationInstance.CompleteRequest()
. 勾选此文章为好,这是从微软KB,推荐使用的Application.CompleteRequest()
替代Response.End()
。
回答by DanielCuadra
I'm adding this so people don't fall into the mistaken accepted response: Response.End() might be required on most case scenarios after transmitting a file.
我添加这个是为了让人们不会陷入错误的接受响应:在传输文件后,在大多数情况下可能需要 Response.End() 。
It doesn't matter if you use TransmitFile, or if you decided to write directly into the output stream, simply most of the times you want to ensure no one can write a single byte after you have sent a file.
无论您是使用 TransmitFile,还是决定直接写入输出流都没有关系,只是在大多数情况下,您希望确保在发送文件后没有人可以写入单个字节。
This is specially important as there will be at some point a filter installed on IIS, a code on global asax EndRequest, or a developer that called your code and then did more things, and any of those will eventually append more stuff into the output stream without you noticing it, and it will corrupt your transmitted file contents. - CompleteRequest won't save you from this, as it allows adding more stuff in the response after you call it.
这一点特别重要,因为在某些时候会在 IIS 上安装过滤器、全局 asax EndRequest 上的代码或调用您的代码然后执行更多操作的开发人员,其中任何一个最终都会将更多内容附加到输出流中在您不注意的情况下,它会破坏您传输的文件内容。- CompleteRequest 不会让您免于此,因为它允许在您调用它后在响应中添加更多内容。
Response.End() is the only way to guarantee no other future code change or IIS filter will manipulate your response as intended.
Response.End() 是保证没有其他未来代码更改或 IIS 过滤器将按预期操作您的响应的唯一方法。