【问题标题】:'Duplicate headers received from server' Error in Chrome 16 with EPPlus 2.9带有 EPPlus 2.9 的 Chrome 16 中的“从服务器收到重复的标头”错误
【发布时间】:2012-01-26 07:03:03
【问题描述】:

我正在玩EPPlus 2.9,由于某种原因,当我尝试使用 Chrome 16 下载单个 .xlsx 文件时遇到 Duplicate headers received from server 错误(它在 IE9 中运行良好)。

我正在使用this tutorial,我已将问题缩小到这行代码:

        Response.AppendHeader("Content-Disposition",
        "attachment; " +
        "filename=\"ExcelReport.xlsx\"; " +
        "size=" + fileBytes.Length.ToString() + "; " +
        "creation-date=" + DateTime.Now.ToString("R") + "; " +
        "modification-date=" + DateTime.Now.ToString("R") + "; " +
        "read-date=" + DateTime.Now.ToString("R"));

我的用户代理:

Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.75 Safari/535.7

我在this Chrome forum page 上读到,Chrome 不喜欢在Content-Disposition 标头中使用逗号 (,),应该将它们替换为分号 (;)。

有人有任何想法或遇到同样的错误吗?

【问题讨论】:

    标签: asp.net excel google-chrome epplus


    【解决方案1】:

    我很笨,DateTime.Now.ToString("R") 产生 Thu, 26 Jan 2012 02:05:44 GMT

    我通过这样做修复了它:

    String timestamp_without_commas = DateTime.Now.ToString("R").Replace(",","");
    
    Response.AppendHeader("Content-Disposition",
        "attachment; " +
        "filename=\"ExcelReport.xlsx\"; " +
        "size=" + fileBytes.Length.ToString() + "; " +
        "creation-date=" + timestamp_without_commas + "; " +
        "modification-date=" + timestamp_without_commas + "; " +
        "read-date=" + timestamp_without_commas);
    

    我习惯了 IE 的暴躁和 Chrome 玩得很好......

    【讨论】:

    • 遇到了类似的问题。我们在文件名中有逗号...谢谢
    【解决方案2】:

    我有同样的问题,我也有正确连接后的分号。我发现我的问题是文件名中有逗号。所以我用破折号代替了它们。

    【讨论】:

      【解决方案3】:
       /// <summary>
              /// ExportToExcel is a method used for Export To Excel with template file.
              ///
              /// </summary>
              /// <param name="templateFile">The fully qualified name of the new file, or the relative file name. Do not end the path with the directory separator character.</param>
              /// <param name="dt">Datatable for export.</param>
              /// <param name="printHeaders">Datatable's header used or not, when Export it. </param>
              /// <param name="exportFileName">provide fileName only not path. </param>
              /// <param name="Response">System.Web.HttpResponse. </param>
              /// <param name="sheetNames">arg[0] means provide sheet name where you want to load data. \n (Optional Parameter) arg[1] means provide sheet name where you want to edit. (Optional Parameter) arg[2] means if your intention is to Edit sheet so provide searchText.</param>
              /// 
              public static string ExportToExcel(FileInfo templateFile, DataTable dt, bool printHeaders, string exportFileName, System.Web.HttpResponse Response, params String[] sheetNames)
              {
                  try
                  {
                      using (ExcelPackage p = new ExcelPackage(templateFile, false))
                      {
                          EPPlus.AddSheetWithTemplate(p, dt, sheetNames[0], printHeaders);
      
      
                          String[] clientName = exportFileName.Split(new char[] { '_' }, 2);
      
                          if (sheetNames.Count() > 2)
                          {
                              ExcelPackagePlusLibrary.EPPlus.EditSheet(p, sheetNames[1], sheetNames[2], clientName[0] ?? exportFileName);
                          }
      
                          Byte[] fileBytes = p.GetAsByteArray(); //Read the Excel file in a byte array
      
                          //Clear the response
                          Response.ClearHeaders();
                          Response.ClearContent();
                          Response.Clear();
      
                          //Response.Cookies.Clear();
      
      
                          //Add the header & other information
                          //Response.Cache.SetCacheability(System.Web.HttpCacheability.Private);
                          //Response.CacheControl = "private";
                          //Response.Charset = System.Text.UTF8Encoding.UTF8.WebName;
                          //Response.ContentEncoding = System.Text.UTF8Encoding.UTF8;
                          //Response.AppendHeader("Content-Length", fileBytes.Length.ToString());
                          //Response.AppendHeader("Pragma", "cache");
                          //Response.AppendHeader("Expires", "60");
                          Response.AddHeader("Content-Disposition",
                          "attachment; " +
                          "filename=" + exportFileName + "; " +
                          "size=" + fileBytes.Length.ToString() + "; " +
                          "creation-date=" + DateTime.Now.ToString("R").Replace(",", "") + "; " +
                          "modification-date=" + DateTime.Now.ToString("R").Replace(",", "") + "; " +
                          "read-date=" + DateTime.Now.ToString("R").Replace(",", ""));
      
                          //Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                          Response.ContentType = "application/x-msexcel";
      
                          //Write it back to the client
                          Response.BinaryWrite(fileBytes);
                          Response.Flush();
                          Response.Close();
      
                          /* Download to Client Side. */
                          //DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/Testing/Downloaded/" + DateTime.Now.ToString("MM-dd-yyyy")));
                          //if (!dir.Exists)
                          //{
                          //    dir.Create();
                          //}
                          //File.WriteAllBytes(dir.FullName + "\\" + fileName, fileBytes);
      
                          return String.Empty;
                      }
                  }
                  catch (Exception ex)
                  {
                      _ErrorMessage = ex.Message.ToString();
                      return _ErrorMessage;
                  }
              }
      

      【讨论】:

      • 仅供参考,文件名周围应该有引号:"filename=" + "\"" + exportFileName + "\"" + ";" 因为 Firefox 中有一个错误,它会如果文件名没有用引号括起来,则在第一个空格之后截断名称。参考kb.mozillazine.org/…
      猜你喜欢
      • 2015-04-09
      • 2012-11-14
      • 2012-01-25
      • 2012-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多