Asp.Net Response.TransmitFile和到Response.End的正确使用() [英] Correct usage of Asp.Net Response.TransmitFile and Response.End()

查看:635
本文介绍了Asp.Net Response.TransmitFile和到Response.End的正确使用()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是什么code的正确用法?

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?

是不是真的很好用 .Flush() .END()

据<一个href=\"http://blogs.msdn.com/b/aspnetue/archive/2010/05/25/response-end-response-close-and-how-customer-feedback-helps-us-improve-msdn-documentation.aspx\">this你永远也不会用到Response.End()(仅在错误或黑客攻击的情况)

According to this you should never ever use Response.End() (only in error or hacking scenarios)

但在某些问题的答案的 .END()的建议印刷...?

But in some of the answers the .End() is reconmended...?

像<一个href=\"http://stackoverflow.com/questions/8570269/using-response-transmitfile-to-download-file-but-also-contains-the-page-source\">this文章

因此​​,它是适合使用到Response.End 或不?

So it is appropriate to use Response.End or not?

推荐答案

我加入这个让人们不要落入错误的接受响应:到Response.End()可能会在之后大多数情况下所需的场景传输文件。

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.

如果您使用的TransmitFile,或者如果你决定直接写入到输出流,只是大部分的时间你要确保没有一个人你发送的文件后,可以写一个字节不要紧。

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.

这是特别重要的,因为会出现在某些时候在IIS上,一个code对全球ASAX EndRequest,或者说叫你code,然后做更多的事情开发商安装了一个过滤器,以及任何这些最终将追加更多的东西到输出流中没有你注意到它,它会破坏你的传送文件内容。
   - CompleteRequest将不会从该救你,因为它允许你调用它后响应添加更多的东西。

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.

到Response.End()是保证没有其他未来code修改或IIS过滤器会操纵你的反应如预期的唯一途径。

Response.End() is the only way to guarantee no other future code change or IIS filter will manipulate your response as intended.

这篇关于Asp.Net Response.TransmitFile和到Response.End的正确使用()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆