失败 – 在下载由EPPlus.dll生成的excel文件时出现networking错误

我尝试从asp.net c#web表单应用程序下载由EPPlus.dll制作的excel文件。 但我得到失败 – networking错误。 应该指出,提到的错误只发生在铬和作业可以在另一个浏览器成功完成。

顺便说一下,这个错误不会发生在我的本地主机,它只发生在主服务器上。

如果有人能够解释这个问题的解决scheme将是非常有益的。

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

尝试这个:

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

当我使用Response.Clear()和Response.Close()时,我遇到了同样的问题,并且必须避免使用下面的代码。

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

我更喜欢不使用response.End(),因为抛出一个exception

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

我遇到了同样的问题,我用下面的代码解决了,注意filename = \“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);