请教各位老大:
我做文件下载,弹出"文件下载对话框",可以保存文件;但是当打开文件时,IE不是弹出一个新IE,而是利用桌面上已经打开的IE,甚至可以会促使页面关闭,请高人指点,解决问题,马上给分!hyperlink.aspx文件,有hyperlinke控件
有如下代码:
protected void Page_Load(object sender, EventArgs e)
{ if (!this.IsPostBack)
{ this.HyperLink1.NavigateUrl = "download.aspx";
this.HyperLink1.Target = "_blank";
this.HyperLink1.Text = "测试文件打开";
this.HyperLink1.Target = "_blank";
}
}
download.aspx.cs文件,有如下代码:
protected void Page_Load(object sender, EventArgs e)
{ if (!this.IsPostBack)
{
try
{
string path = Server.MapPath("linshilanmu3.jpg");
if (!System.IO.File.Exists(path))
{ this.Page.Response.End();
} System.IO.FileInfo file = new System.IO.FileInfo(path); this.Page.Response.Clear();
this.Page.Response.Buffer = false;
this.Page.Response.AddHeader("Content-Disposition", "attachment;filename=" + file.Name);
this.Page.Response.AddHeader("Content-Length", file.Length.ToString());
#region//文件下载类型
string filename = "linshilanmu3.jpg";
string strFilePostfix = filename.Substring(filename.Length - 4, 4).ToLower();
if (strFilePostfix == ".asf")
this.Response.ContentType = "video/x-ms-asf";
else if (strFilePostfix == ".avi")
this.Response.ContentType = "video/avi";
else if (strFilePostfix == ".doc")
this.Response.ContentType = "application/msword";
else if (strFilePostfix == ".zip")
this.Response.ContentType = "application/zip";
else if (strFilePostfix == ".xls")
this.Response.ContentType = "application/vnd.ms-excel";
else if (strFilePostfix == ".avi")
this.Response.ContentType = "video/avi";
else if (strFilePostfix == ".gif")
this.Response.ContentType = "image/gif";
else if (strFilePostfix == ".jpg" || strFilePostfix == ".jpeg")
this.Response.ContentType = "image/jpeg";
else if (strFilePostfix == "audio/wav")
this.Response.ContentType = "audio/wav";
else if (strFilePostfix == ".mpg" || strFilePostfix == ".mpeg")
this.Response.ContentType = "video/mpeg";
else if (strFilePostfix == ".rtf")
this.Response.ContentType = "application/rtf";
else if (strFilePostfix == ".htm" || strFilePostfix == ".html")
this.Response.ContentType = "text/html";
else if (strFilePostfix == ".txt")
this.Response.ContentType = "text/plain";
else
this.Response.ContentType = "application/octet-stream";
#endregion
this.Page.Response.WriteFile(file.FullName);
this.Response.Flush(); this.Page.Response.End();
}
catch
{ this.Page.Response.Write("附件不存在或者已经被删除!");
} }
}
我做文件下载,弹出"文件下载对话框",可以保存文件;但是当打开文件时,IE不是弹出一个新IE,而是利用桌面上已经打开的IE,甚至可以会促使页面关闭,请高人指点,解决问题,马上给分!hyperlink.aspx文件,有hyperlinke控件
有如下代码:
protected void Page_Load(object sender, EventArgs e)
{ if (!this.IsPostBack)
{ this.HyperLink1.NavigateUrl = "download.aspx";
this.HyperLink1.Target = "_blank";
this.HyperLink1.Text = "测试文件打开";
this.HyperLink1.Target = "_blank";
}
}
download.aspx.cs文件,有如下代码:
protected void Page_Load(object sender, EventArgs e)
{ if (!this.IsPostBack)
{
try
{
string path = Server.MapPath("linshilanmu3.jpg");
if (!System.IO.File.Exists(path))
{ this.Page.Response.End();
} System.IO.FileInfo file = new System.IO.FileInfo(path); this.Page.Response.Clear();
this.Page.Response.Buffer = false;
this.Page.Response.AddHeader("Content-Disposition", "attachment;filename=" + file.Name);
this.Page.Response.AddHeader("Content-Length", file.Length.ToString());
#region//文件下载类型
string filename = "linshilanmu3.jpg";
string strFilePostfix = filename.Substring(filename.Length - 4, 4).ToLower();
if (strFilePostfix == ".asf")
this.Response.ContentType = "video/x-ms-asf";
else if (strFilePostfix == ".avi")
this.Response.ContentType = "video/avi";
else if (strFilePostfix == ".doc")
this.Response.ContentType = "application/msword";
else if (strFilePostfix == ".zip")
this.Response.ContentType = "application/zip";
else if (strFilePostfix == ".xls")
this.Response.ContentType = "application/vnd.ms-excel";
else if (strFilePostfix == ".avi")
this.Response.ContentType = "video/avi";
else if (strFilePostfix == ".gif")
this.Response.ContentType = "image/gif";
else if (strFilePostfix == ".jpg" || strFilePostfix == ".jpeg")
this.Response.ContentType = "image/jpeg";
else if (strFilePostfix == "audio/wav")
this.Response.ContentType = "audio/wav";
else if (strFilePostfix == ".mpg" || strFilePostfix == ".mpeg")
this.Response.ContentType = "video/mpeg";
else if (strFilePostfix == ".rtf")
this.Response.ContentType = "application/rtf";
else if (strFilePostfix == ".htm" || strFilePostfix == ".html")
this.Response.ContentType = "text/html";
else if (strFilePostfix == ".txt")
this.Response.ContentType = "text/plain";
else
this.Response.ContentType = "application/octet-stream";
#endregion
this.Page.Response.WriteFile(file.FullName);
this.Response.Flush(); this.Page.Response.End();
}
catch
{ this.Page.Response.Write("附件不存在或者已经被删除!");
} }
}
Response.Write("<script>window.open(\"download.aspx\")</script>")
Response.Write("<script>window.open(\"download.aspx\")</script>")
,这个方法还是无法解决,不过谢谢您的关注
你应该用linkbutton代替this.HyperLink1
然后再linkbutton的click事件里写
protected void LinkButton1_Click(object sender, EventArgs e)
{
this.Response.Write("<script>window.open('download.aspx')</script>");
}也不能解决啊