ASP.NET直接下载一个文件,而不是在IE中打开它
有的时候我们不想让用户直接在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();
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 这个名字的。