2016-09-21 42 views
9

我嘗試從asp.net c#web表單應用程序下載由EPPlus.dll製作的excel文件。但我得到失敗 - 網絡錯誤。 應該指出,提到的錯誤只發生在鉻和作業可以在另一個瀏覽器中成功完成。失敗 - 當下載由EPPlus.dll製作的excel文件時出現網絡錯誤

順便說一句,這個錯誤不會發生在我的本地主機上,它只發生在主服務器上。

如果有人能夠解釋這個問題的解決方案,這將是非常有益的。

http://www.irandnn.ir/blog/PostId/29/epplus

+0

po st點擊事件的代碼或者這個文件實際發送給客戶端的東西。 – Rex

回答

5

試試這個:

using (ExcelPackage p = new ExcelPackage()) 
{ 
    //Code to fill Excel file with data. 


    Byte[] bin = p.GetAsByteArray(); 

    Response.ClearHeaders(); 
    Response.ClearContent(); 
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
    Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", Nombre_Del_Libro + ".xlsx")); 
    Response.BinaryWrite(bin); 
    Response.Flush(); 
    Response.End(); 
} 
12

我有同樣的問題我是用Response.Clear()和Response.Close()時,不得不避免他們看看我的代碼如下這是工作。

Response.Buffer = true; 
Response.ContentType = mimeType; 
Response.AddHeader("Content-Disposition", "attachment; filename=" + nameOfFile); 
Response.BinaryWrite(bytes); 
Response.End(); 
0

我有同樣的問題,我用下面的代碼解決了,注意引號中的文件名= \ 「Name.xlsx \」:

Response.Buffer = true; 
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
Response.AppendHeader("content-disposition", "attachment; filename=\"Name.xlsx\""); 
//Writeout the Content 
Response.BinaryWrite(bytes); 
1

我不喜歡使用response.End(),因爲拋出異常

protected void DownloadFile(FileInfo downloadFile, string downloadFilename, string downloadContentType) 
    { 
     Byte[] bin = File.ReadAllBytes(downloadFile.FullName); 

     Response.ClearHeaders(); 
     Response.ClearContent(); 
     Response.ContentType = downloadContentType; 
     Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", downloadFilename)); 
     Response.BinaryWrite(bin); 
     Response.Flush(); 
     Response.SuppressContent = true; 
    } 
+1

這解決了我的問題。謝謝 – Sergio

相關問題