System.IO.FileInfo info = new System.IO.FileInfo(Server.Mappath("Data\") + "FileName.exe");
protected void Button3_Click(object sender, EventArgs e)
{
Response.Clear();
Response.ClearHeaders();
Response.Buffer = true;
// 指定返回的是一个不能被客户端读取的流,必须被下载
Response.ContentType = "application/octet-stream";
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(info.Name, System.Text.Encoding.UTF8));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
Response.AddHeader("Content-Length", info.Length.ToString());
//Response.AddHeader("Content-Transfer-Encoding", "binary");
// 指定返回的是一个不能被客户端读取的流,必须被下载
//Response.AddHeader("content-type", "application/x-msdownload");
// 指定返回的是一个不能被客户端读取的流,必须被下载
// Response.ContentType = "application/ms-excel";
// 把文件流发送到客户端
Response.WriteFile(info.FullName);
Response.Flush();
Response.End();
}
文件下载,指定默认名
Response.AddHeader("content-type","application/x-msdownload");
Response.AddHeader("Content-Disposition","attachment;filename=要下载的文件名.rar");
刷新页面
Response.AddHeader “REFRESH”, ”60;URL=newpath/newpage.asp”
这等同于客户机端<META>元素:
<META HTTP-EQUIV=”REFRESH”, “60;URL=newpath/newpage.asp”
页面转向
Response.Status = “302 Object Moved”
Response.Addheader “Location”, “newpath/newpage.asp”
这等同于使用Response.Redirect方法:
Response.Redirect “newpath/newpage.asp”
强制浏览器显示一个用户名/口令对话框
Response.Status= “401 Unauthorized”
Response.Addheader “WWW-Authenticate”, “BASIC”
强制浏览器显示一个用户名/口令对话框,然后使用BASIC验证把它们发送回服务器(将在本书后续部分看到验证方法)。
如何让网页不缓冲
Response.Expires = 0
Response.ExpiresAbsolute = Now() - 1
Response.Addheader "pragma","no-cache"
Response.Addheader "cache-control","private"
Response.CacheControl = "no-cache"