发送Excel文件给使用Chrome和IE但不使用Firefox的用户(获取没有扩展名的文件)

我使用OpenXML库生成和excel文件并将其发送给用户。 它与Chrome和IE工作得很好,但是当我用Firefox试了一下,我遇到了一个问题。

当使用FF保存文件时,我得到一个没有扩展名的文件当使用FF打开文件时,它像一个魅力:(

我正在使用以下function将stream发送给用户。

public static void SendToClient(Byte[] stream, string fileName) { HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; HttpContext.Current.Response.BinaryWrite(stream); HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + fileName + ".xlsx"); HttpContext.Current.Response.AddHeader("content-length", stream.LongLength.ToString()); HttpContext.Current.Response.End(); } 

并像这样调用它:

 _ExcelReports.SendToClient(excelUtil.ExportToExcel(excelWorkBook), projectName + " Resources"); 

即使更奇怪,FF下载对话框识别文件,如截图:

FF下载对话框

现在完成下载:

FF完成下载

你有没有尝试过使用contentType = "application/vnd.ms-excel"

我是一个有更大问题的新手,弄清楚我正在做什么,但是我正在使用的一点是使用这种内容types。

该代码示例为我工作。 所以是时候给一些随机的debuggingbuild议了。

1)尝试清除Firefox的历史logging/ Cookie /内容caching。 然后再运行一次。 caching可能会污染响应。

2)SendToClient()函数是最后一个运行的函数吗? 因为如果在它之后还有别的事情发生(因为它包含了Response.End()函数),一些奇怪的东西可能会发生或损坏响应stream。

3)尝试评论设置ContentEncoding的代码行和添加内容长度的代码行。 事实上,尝试这SendToClient()(为我工作的最低限度):

 HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; HttpContext.Current.Response.BinaryWrite(stream); HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + fileName + ".xlsx"); HttpContext.Current.Response.End(); 

基本上,这是testing仍然适用于IE和Chrome,但FF失败的最小代码行。

4)尝试“内联”,而不是“附件”的内容configuration ,看看是否工作的第一。

当然,我假设OpenXML文件本身是有效的。 当您打开下载的文件(无论是使用IE还是Chrome)时,Excel是否会抱怨(比如说要求修复文件)?