Yogesh
Yogesh

Reputation: 2228

ASP.NET MVC cannot stream files

I have a ASP.NET MVC 4 site that creates an excel file using OPEN XML SDK. I simply point the hyperlink to the proper controller and it generates the OPEN XML excel document and writes the stream to response header and done. In IE 9 and Chrome this works fine. File gets downloaded with the given file name and proper contents. However, just recently I upgraded my browser to IE 10 and now instead of downloading the file and opening up in excel I get the error that could not open 'URI'. When I click ok it gives another error: Microsoft Excel cannot access the file 'URI'. There are several possible reasons:

I don't understand why this would work in IE 9 and chrome and not in IE 10. I debugged the response headers with fiddlers and it has the proper content type, and content length set:

Content Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

Content Disposition: attachment; filename=result.xlsx

Content length: 1232

Is there something that I am missing?

Code snippet: This all is part of

public override void ExecuteResult(ControllerContext context)
{
...
....
..
    extention = "xlsx";
    response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    response.AddHeader("Content-Disposition",
                                    String.Format("attachment; filename={0}.{1}", fileName, extention));
    response.AddHeader("Content-Length", mem.Length.ToString());
    mem.Seek(0, SeekOrigin.Begin); // Go back to the begining.
    mem.CopyTo(response.OutputStream);
    context.HttpContext.ApplicationInstance.CompleteRequest();
}

Upvotes: 4

Views: 1935

Answers (3)

cchdev
cchdev

Reputation: 501

As a workaround, Controller.File works for me with ASP.NET MVC 4 and IE 10:

public class DownloadController : Controller
{
    public ActionResult GetFile()
    {
        ...

        mem.Position = 0;

        return File(
            mem, 
            "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", 
            "result.xlsx");
    }
}

EDIT

So I have been browsing the MVC source code at CodePlex and found a few implementation details that differ from your code snippet:

  • Content-Length header is never set

  • Instead of calling Stream.CopyTo, they use a simple Stream.Read, Stream.Write loop with buffer size of 0x1000

  • When setting Content-Disposition header, they check whether file download name contains UTF-8 chars, and if so, encode them according to RFC 2231. See ContentDispositionUtil.GetHeaderValue

  • context.HttpContext.ApplicationInstance.CompleteRequest() is never called

Now you could try applying these changes one by one and see which one makes it work with IE 10.

Upvotes: 2

Devendra Soni
Devendra Soni

Reputation: 1934

try these MIME types:- for old version:-

Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("content-disposition", "attachment; filename=myfile.xls");

for 2007:-

Response.ContentType = "application/application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AppendHeader("content-disposition", "attachment; filename=result.xlsx");

for csv files which you need to open in excel is:-

Response.AddHeader "Content-Disposition", "Attachment;Filename=myfile.csv"

in last you can try:-

Response.ContentType = "application/octet-stream";
Response.AppendHeader("content-disposition", "attachment; filename=result.xlsx");

thanks

Upvotes: 0

Ivan Samygin
Ivan Samygin

Reputation: 4581

I guess it's an IE10 bug. But maybe you still can work around it.

Based on this post and a few others. Please check that:

  1. Both full URI and the filename of Content Disposition header do not contains special symbols and that their lengths are less than 105. Just use regular A-Za-z0-9 symbols for instance.

  2. Try to add "X-UA-Compatible" HTTP header with "IE=9" value.

Upvotes: 0

Related Questions