服务器上有一现成文件tempdata.txt,希望下载到用户端形成文件。(不是在浏览器里打开)
我在右框架用
string temp = string.Format("attachment;filename={0}", HttpUtility.UrlEncode(strOutputFileName,Encoding.UTF8 ));
Response.AppendHeader("Content-disposition", temp);
Response.AppendHeader("CONTENT-ENCODING","GB2312");
Response.WriteFile(System.IO.Path.GetFullPath(Server.MapPath(".")+"\\callticketbackup\\"+"tempdata.txt"));
实现文件下载后,可能因为Header改变了,有些控件不能使用,出现“错误代码”的提示。左框架内的TreeView也改变不了右框架的内容了。
手动刷新页面后一切正常。
但代码无法刷新页面。有人说:把下载文件的代码放在另一个页面,把这个页面放在一个隐藏的框架中,点击右
框架的下载按钮后,改变隐藏框加析url到下载页面。
不知具体是如何实现。还有更好的办法吗?总觉得应该有更简便的方法哦!请给出C#示例代码,我比较菜,呵呵。分不够的话我可以另开贴子答谢!谢谢先!

解决方案 »

  1.   

    Response.ClearHeaders();
    Response.BufferOutput=true;
    Response.Charset="GB2312";
    Response.ContentType="application/ms-excel";
    Response.AppendHeader("Content-Disposition","attachment;filename="+name+".xls");
    Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
    this.EnableViewState=false;
    System.Globalization.CultureInfo myInfo=new System.Globalization.CultureInfo("ZH-CN",true);
    System.IO.StringWriter osw=new System.IO.StringWriter(myInfo);
    System.Web.UI.HtmlTextWriter htw=new System.Web.UI.HtmlTextWriter(osw);
    this.DataList1.RenderControl(htw);
    HttpContext.Current.Response.Write(osw.ToString());
    Response.Flush();
    Response.End();
      

  2.   

    上面的是存为excel文件,这个是下载任意文件
    string fullName=currentPath+name;

    FileInfo aFile=new FileInfo(fullName); Response.Clear();
    Response.ClearHeaders();
    Response.BufferOutput=false;
    Response.ContentType="application/octet-stream";
    Response.AppendHeader("Content-disposition","attachment;filename="+HttpUtility.UrlEncode(name,System.Text.Encoding.UTF8));
    Response.AppendHeader("Content-Length",aFile.Length.ToString());
    Response.WriteFile(fullName);
    Response.Flush();
    Response.End();
      

  3.   

    To:cocoguo(周周)你贴出的代码和我现在用的代码没有本质上的区别。一样有后遗症。
    我问的是下完文件后,页面不正常如何处理啊……
      

  4.   

    突然想到,我修改过的代码试一下!Response.Clear();
    Response.ClearHeaders();
    Response.BufferOutput=false;
    Response.ContentType="application/octet-stream";
    Response.AppendHeader("Content-disposition","attachment;filename="+HttpUtility.UrlEncode(name,System.Text.Encoding.UTF8));
    Response.AppendHeader("Content-Length",aFile.Length.ToString());
    Response.WriteFile(fullName);
    Response.Flush();
    //以下增加
    Response.Clear();
    Response.ClearHeaders();
    Response.ContentType="text/html";
    Response.Write("<script> document.location='你的目标.' </script>")
    Response.End();
      

  5.   

    http://community.csdn.net/Expert/topic/3796/3796663.xml?temp=.7320215
    .cs里有个down()函数来输出下载文件,
    FileInfo file1 = new FileInfo(file0); 
    Response.Clear(); 
    Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(file1.Name)); 
    Response.WriteFile(file1.FullName); 
    Response.End();添加一个按钮,点击button1时来调用这个函数
    你可以在html页面中定义一个js函数downfile来点击button1按钮
    function downfile()
    {
    document.all("button1").click();
    }
    然后在<body onload="downfile">