protected void Page_Load(object sender, EventArgs e)

     string downLoadPath = @"D:\4Deploy\1.docx";
     Response.Clear();
     Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "1.docx"));
     Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
     Response.TransmitFile(downLoadPath); 
     HttpContext.Current.Response.End(); 
}核心代码就上面了, 我是从一个页面window.open("downLoad.aspx"); 调试环境下可以下载,到了测试环境下的时候就无法下载了,页面一闪没了,没有进行下载。代码是从老项目中100%copy过来的,现在把网上的办法都试过了。没效果

解决方案 »

  1.   

    context.Response.ContentType = "application/octet-stream";
      

  2.   

    另外下载的时候将context.Response.ContentType = "application/octet-stream";放到你的AddHeader之前
      

  3.   


     protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string fileName = "XXX.docx";//客户端保存的文件名
                string filePath = Server.MapPath("test.docx");//路径            FileInfo fileInfo = new FileInfo(filePath);
                Response.Clear();
                Response.ClearContent();
                Response.ClearHeaders();
                Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
                Response.AddHeader("Content-Length", fileInfo.Length.ToString());
                Response.AddHeader("Content-Transfer-Encoding", "binary");
                Response.ContentType = "application/octet-stream";
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
                Response.WriteFile(fileInfo.FullName);
                Response.Flush();
                Response.End();
            }        
        }
      

  4.   

    ZIP文件时要改些什么?  
      

  5.   


    string fileName = "XXX.ZIP";//客户端保存的文件名
    string filePath = Server.MapPath("test.ZIP");//路径
      

  6.   

    以上办法都没法解决此问题, 还是靠自己花了一天时间发现了。很不经意的一个小问题,就是在转入下载链接之前不能加了alert()之类的提示。去掉之后就正常了,但是新的问题又来了下载大文件就没折了,超过700M的文件就崩溃