一下所写的代码都是在下载页面page_load事件中:
//直接输出文件形式
Response.Clear(); Response.Buffer = true; Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Server.MapPath("~/Global.asax"),System.Text.Encoding.UTF8)); Response.WriteFile(Server.MapPath("~/Global.asax")); Response.Flush(); Response.Close();
//输出文件流形式
FileStream fileStream = new FileStream(Server.MapPath("~/Global.asax"), FileMode.Open);
long fileSize = fileStream.Length; byte[] fileBuffer = new byte[fileSize]; fileStream.Read(fileBuffer, 0, (int)fileSize);
//如果不写fileStream.Close()语句,用户在下载过程中选择取消,将不能再次下载 fileStream.Close(); Response.ContentType = "application/octet-stream"; Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Server.MapPath("~/Global.asax"), System.Text.Encoding.UTF8)); Response.AddHeader("Content-Length", fileSize.ToString()); Response.BinaryWrite(fileBuffer); Response.Flush(); Response.Close();