日期:2014-05-17  浏览次数:20443 次

c# 文件输出后如何关闭页面?
string filename = DropDownList1.SelectedItem.Text;//获取用户选择的文件输出名称
FileInfo file = new FileInfo(filename);//创建一个文件对象
Response.Clear();//清除所有缓存区的内容
Response.Charset = "GB2312";//定义输出字符集
Response.ContentEncoding = Encoding.Default;//输出内容的编码为默认编码
Response.AddHeader("Content-Disposition","attachment;filename="+file.Name);//添加头信息。为“文件下载/另存为”指定默认文件名称
Response.AddHeader("Content-Length",file.Length.ToString());//添加头文件,指定文件的大小,让浏览器显示文件下载的速度
Response.WriteFile(file.FullName);// 把文件流发送到客户端
Response.End();//将当前所有缓冲区的输出内容发送到客户端,并停止页面的执行

文件输出后,页面还是开着,如何关闭这个页面呢?
我在response.end()前面加了一句:Response.Write("<script>window.opener=null;window.close();</script>");但是不管用~~~~~
------最佳解决方案--------------------
如果是非frame窗体,则使用的window.close();
Response.Write(" <script>window.close(); </script>");// 会弹出询问是否关闭 
Response.Write(" <script>window.opener=null;window.close(); </script>");// 不会弹出询问
如果是frame窗体,则使用的window.parent.close();
string script = " <script language='javascript'>window.open('../Default.aspx');" + 
            "window.parent.opener = null;window.parent.open('','_self');window.parent.close(); window.close(); </script>"; 
        page.ClientScript.RegisterClientScriptBlock(page.GetType(), "CloseWindow", script);
------其他解决方案--------------------
this.Close();
/
this.Visible=false; 
------其他解决方案--------------------
Response.End();//

这句代码后面的就不执行了。所以他后面的代码跟没不起作用。你根据需要调整下代码位置
------其他解决方案--------------------
JS关闭,有些浏览器是不支持的,只输出纯文件输出看看
------其他解决方案--------------------
如果是弹出div页面的话,设置css样式为display:none就可以了。
------其他解决方案--------------------
downfile.aspx页面 

上面是我这个页面里所有的代码了

您说的this.close(); 无效呢~