2016-09-21 77 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

相关问题