Sna*_*ake 3 c# header http-headers
我们在ASP.NET服务器上提供文件时遇到了一个奇怪的问题.
如果用户单击链接,我们希望有一个文件下载对话框.没有针对WMV的WMP开放,没有针对PDF的Adobe开放,等等.
为了强制这个,我们使用以下HTTP处理程序来跳转WMV,PDF等.
public void ProcessRequest(HttpContext context)
{
// don't allow caching
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.Cache.SetNoStore();
context.Response.Cache.SetExpires(DateTime.MinValue);
string contentDisposition = string.Format("attachment; filename=\"{0}\"", Path.GetFileName(context.Request.PhysicalPath));
string contentLength;
using (FileStream fileStream = File.OpenRead(context.Request.PhysicalPath))
{
contentLength = fileStream.Length.ToString(CultureInfo.InvariantCulture);
}
context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("Content-Disposition", contentDisposition);
context.Response.AddHeader("Content-Length", contentLength);
context.Response.AddHeader("Content-Description", "File Transfer");
context.Response.AddHeader("Content-Transfer-Encoding", "binary");
context.Response.TransmitFile(context.Request.PhysicalPath);
}
Run Code Online (Sandbox Code Playgroud)
用小提琴嗅闻,这些是发送的实际标题:
HTTP/1.1 200 OK
Cache-Control: no-cache, no-store
Pragma: no-cache
Content-Length: 8661299
Content-Type: application/octet-stream
Expires: -1
Server: Microsoft-IIS/7.5
Content-Disposition: attachment; filename="foo.wmv"
Content-Description: File Transfer
Content-Transfer-Encoding: binary
X-Powered-By: ASP.NET
Date: Wed, 04 Apr 2012 09:38:14 GMT
Run Code Online (Sandbox Code Playgroud)
但是,当我们点击WMV链接时,这仍然会打开WMP,同样对于Adobe Reader,它仍会在IE窗口中打开Adobe Reader.
Firefox上似乎没有出现此问题,但它出现在Windows 7(32位)上的IE8(32位)上.
有帮助吗?
更换
context.Response.ContentType = "application/octet-stream";
Run Code Online (Sandbox Code Playgroud)
同
context.Response.ContentType = "application/force-download";
Run Code Online (Sandbox Code Playgroud)
看看它做了什么,不知道它是否适用于所有浏览器.
| 归档时间: |
|
| 查看次数: |
8083 次 |
| 最近记录: |