Question

I am generating and excel file using the OpenXML library and sending it to the user. It works very well with Chrome and IE but when i tried it with Firefox i came upon a problem.

When saving the file using FF i get a File with no extension When opening the file using FF it works like a charm :(

I am using the following function to send the stream to the user.

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

and calling it like this:

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

Even weirder, FF download dialog recognizes the file, like in screenshot:

FF download dialog

Now for the completed download:

FF completed downloads

Was it helpful?

Solution

Have you tried using contentType = "application/vnd.ms-excel" ?

I'm a newbie with bigger problems figuring out what I'm trying to do, but the little bit that I have working is using that content type.

OTHER TIPS

That code sample worked for me. So it's time for some random debugging suggestions.

1) Try clearing the history/cookie/content cache for Firefox. Then run it again. The cache might be fouling up the response.

2) Is the SendToClient() function the very last function to be run? Because if there's anything else done after it (because it contains the Response.End() function), some weird stuff might happen or corrupt the response stream.

3) Try commenting the code line for setting the ContentEncoding, and the code line for adding the content-length. In fact, try this for SendToClient() (the bare minimum that worked for me):

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

Basically, it's to test the minimum lines of code that still work for IE and Chrome but fail for FF.

4) Try "inline" instead of "attachment" for content-disposition and see if that works first.

Of course, I'm assuming the OpenXML file itself is valid. Does Excel complain (say asking to repair the file or something) when you open the downloaded file (whether from using IE or Chrome)?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top