求助!用C#.net做文件下载 出现打开下载对话框,主浏览器窗口自动关闭的问题。
<ItemTemplate>
<a href= '../download_Attach.aspx?type=T&dir_id= <%=SessionUserDirID%> &file_id= <%#DataBinder.Eval(Container.DataItem, "ATTACH_ID ")%> '>
<%#DataBinder.Eval(Container.DataItem, "FILE_NAME ")%>
<%#DataBinder.Eval(Container.DataItem, "FILE_EXT ")%>
</a>
</ItemTemplate>
上面是GridView的一个模版列的内容 我想用这个链接下载文件 但是一点链接 弹出下载对话框的同时,主窗口就关闭了 有时候也不关 很无常,但是一旦不关了,再怎么弄也不关,不同机器还不一样。是程序的毛病还是浏览器的设置问题呢?请问哪位帮帮忙 在线等待……谢谢!
下面是download_Attach.aspx的Html内容
<%@ Page Language= "C# " AutoEventWireup= "true " CodeFile= "download_Attach.aspx.cs " Inherits= "OilPipe.PublicAffairManage.download_AplAttach " %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> OILPIPE </title>
</head>
<body onload= 'window.open( "_blank ", " ", "height=1,width=1,top=180,left=320;status:no;help:no;menubar=no; ") '>
<form id= "form1 " runat= "server ">
<div>
</div>
</form>
</body>
</html>
下面是download_Attach.aspx的后台代码:
namespace PublicAffairManage
{
public partial class download_AplAttach : PublicInfo
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session[ "USER_NAME "] == null) Response.End();
string FileName = string.Empty;
string FileExName = string.Empty;
string FileType = string.Empty;
string FilePath = string.Empty;
string str_ATTACH_ID = IsNull(Request.QueryString[ "file_id "]);
string str_file_dirid = IsNull(Request.QueryString[ "dir_id "]);
string str_type = IsNull(Request.QueryString[ "type "]);
//获得文件所在目录
OP_DIR srv_dir = new OP_DIR();
DataTable dt_dirInfo = srv_dir.GetUserFolder_S(str_file_dirid);
if(dt_dirInfo.Rows.Count> 0)
{
string disk_path = Server.MapPath( "OilPipe ").Substring(0, 1);
string dir_fromDB = IsNull(dt_dirInfo.Rows[0][ "DIR_PATH "]);
FilePath = disk_path + dir_fromDB.Substring(1, dir_fromDB.Length-1) + IsNull(dt_dirInfo.Rows[0][ "DIR_NAME "]) + "\\ ";
}
//获得文件物理信息
DataTable dt_attach;
switch (str_type)
{
case "A ":
dt_attach = new OP_APL_ATTACH().GetFilePhysicalInformation_S(str_ATTACH_ID);
break;
case "D ":
dt_attach = new OP_APR_ATTACH().GetFilePhysicalInformation_S(str_ATTACH_ID);
break;
case "T ":
dt_attach = new OP_ITR_ATTACH().GetFilePhysicalInformation_S(str_ATTACH_ID);
break;
case "E ":
dt_attach = new OP_EXE_ATTACH().GetFilePhysicalInformation_S(str_ATTACH_ID);
break;
default: return;
}
if (dt_attach.Rows.Count > 0)
{
FileName = IsNull(dt_attach.Rows[0][ "FILE_NAME "]);
FileExName = IsNull(dt_attach.Rows[0][ "FILE_EXT "]);
FileType = IsNull(dt_attach.Rows[0][ "FILE_EXT "]);
}
//组合文件全路径
string FileFullPath = FilePath + FileName + FileExName;
if (File.Exists(FileFullPath))
{
System.IO.FileStream stream = new FileStream(FileFullPath, FileMode.Open, FileAccess.Read); //打开文件流
byte[] byt = new byte[(int)stream.Length]; //定义一个与文件长度相等长度的字节数组
stream.Read(byt, 0, byt.Length); //把打开的文件流读入到这个数组里
stream.Close(); //读了文件流必需要关闭,要不然会报错
Context.Response.Buffer = true; //设置缓冲输出,全部完成之后在输出
Context.Response.ContentType = FileType; //设置文件流的类型
//生成保存合法的文件名
string FileSaveName = " ";
if (FileName.Length > 16)
{
FileName = FileName.Substring(0, 16);
}
FileSaveName = FileName + FileExName;
Context.Response.AddHeader( "Content-Disposition ", "attachment;filename= " + HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(FileSaveName)));
Context.Response.BinaryWrite(byt);
}
else
{
Page.RegisterClientScriptBlock( " ", " <script> alert(\ "找不到所要下载的文件!\ "); </script> ");
}
}
}
}
}
[解决办法]
需要新open一个窗口下载
记得xp的机器有这种情况