小弟不才,用一页面专门作为下载,代码如下.通过超链接到这个页面时,在下载框出现的同时也在背后出现一空白页面,如何只显示下载框,而不显示背后的一空白页面?谢过,先
protected void Page_Load(object sender, EventArgs e)
{
string filename = Session["name"].ToString(); string path = Server.MapPath("file/") + filename;
FileInfo fi = new FileInfo(path); //判断文件是否存在
if (fi.Exists)
{
//将文件保存到本机上
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(fi.Name));
Response.AddHeader("Content-Length", fi.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.Filter.Close();
Response.WriteFile(fi.FullName);
Response.End();
}
}
protected void Page_Load(object sender, EventArgs e)
{
string filename = Session["name"].ToString(); string path = Server.MapPath("file/") + filename;
FileInfo fi = new FileInfo(path); //判断文件是否存在
if (fi.Exists)
{
//将文件保存到本机上
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(fi.Name));
Response.AddHeader("Content-Length", fi.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.Filter.Close();
Response.WriteFile(fi.FullName);
Response.End();
}
}
超链接的时候 加上_black
int DiskId = Request.QueryString["DiskId"] == null ? 0 : int.Parse(Request.QueryString["DiskId"].ToString());
string FileName = Request.QueryString["FileName"] == null ? "" : HttpUtility.UrlDecode(Request.QueryString["FileName"].ToString());
string dir = Server.MapPath("../path") + DiskId.ToString();
Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(FileName.Substring(FileName.LastIndexOf('/') + 1), System.Text.Encoding.GetEncoding("utf-8")));
Response.ContentType = "application/octet-stream";
Response.WriteFile("" + dir.Trim() + FileName+"");
Response.End();
不要单独导航到这个页面,在原始页面嵌入一个IFrame并且动态设置它的src引导到这个下载页面就可以了。