ASP.NET直接下载一个文件,而不是在IE中打开它

类别:Asp 点击:0 评论:0 推荐:

有的时候我们不想让用户直接在IE中打开已知类型的文件,比如Word,而希望能直接下载,这时候可用下面代码来替换Response.Redirect

Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;FileName="+YourFileName);
Response.BinaryWrite((byte[])YourFileData.Rows[0]["AttachmentContent"]);
Response.End();


 

补充一下。通常我喜欢把链接做成这样:

<a href="download.aspx/hello.chm?fileid=12345">hello.chm</a>

这样客户端下载的时候默认是 hello.chm 这个名字的。

本文地址:http://com.8s8s.com/it/it8231.htm