有的时候我们不想让用户直接在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();
回复: [tip 1001]ASP.NET直接下载一个文件,而不是在IE中打开它
----下面是我的代码--------------------------------------------------------------------------------------------
int id = int.Parse(context.Request.QueryString["id"]);
string sendUserNo = context.Request.QueryString["sendUserNo"];
string receiveUserNo = context.Request.QueryString["receiveUserNo"];
Message message = new Message(id, sendUserNo, receiveUserNo);
string name = context.Server.UrlEncode(message.FileName);
string oppositeName = message.Filepath;
string absoluteName = context.Server.MapPath(message.Filepath);
FileStream fileStream = new FileStream(absoluteName, FileMode.Open,FileAccess.Read);
long fileSize = fileStream.Length;
context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("Content-Disposition", "attachment;FileName=" + name);
byte[] fileBuffer = new byte[fileSize];
fileStream.Read(fileBuffer, 0, (int)fileSize);
context.Response.BinaryWrite(fileBuffer);
context.Response.End();
posted on 2006-07-20 16:58
阿米 阅读(2353)
评论(2) 编辑 收藏 网摘 所属分类:
.NET