2012-01-26 21 views
5

Tôi đang phát xung quanh với EPPlus 2.9 và vì một số lý do tôi gặp phải lỗi Duplicate headers received from server khi tôi cố tải xuống một tệp .xlsx bằng Chrome 16 (Nó hoạt động tốt IE9).'Tiêu đề trùng lặp nhận được từ máy chủ' Lỗi trong Chrome 16 với EPPlus 2.9

Tôi đang sử dụng this tutorial và tôi đã thu hẹp vấn đề để dòng mã này:

 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")); 

useragent của tôi:

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

Tôi đọc trên this Chrome forum page rằng Chrome không thích dấu phẩy (,) trong các tiêu đề Content-Disposition và chúng phải được thay thế bằng dấu chấm phẩy (;).

Bất kỳ ai có bất kỳ ý tưởng hoặc nhận được cùng một lỗi?

Trả lời

9

Tôi câm, DateTime.Now.ToString("R") sản xuất Thu, 26 Jan 2012 02:05:44 GMT

tôi cố định nó bằng cách làm này:

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); 

Tôi đã quen với IE là cáu kỉnh và Chrome chơi đẹp ...

+3

Đã xảy ra sự cố tương tự. Chúng tôi đã có dấu phẩy trong tên tệp ... Cảm ơn – NotMe

0
/// <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; 
      } 
     } 
+0

Chỉ để biết thông tin, tên tệp phải có dấu ngoặc kép xung quanh chúng: "filename =" + "\" "+ exportFileName +" \ "" + ";" vì có lỗi trong Firefox nơi nó sẽ cắt ngắn tên sau khi không gian đầu tiên nếu tên tập tin không được bao quanh với dấu ngoặc kép. Tham khảo http://kb.mozillazine.org/Filenames_with_spaces_are_truncated_upon_download –

3

Tôi đã gặp vấn đề tương tự và tôi cũng đã có dấu chấm phẩy sau khi đính kèm chính xác. Tôi thấy vấn đề của mình là có dấu phẩy trong tên tệp. Vì vậy, tôi đã thay thế bằng dấu gạch ngang.

+0

Khắc phục vấn đề – Balamurugan

Các vấn đề liên quan