1

I've been trying to get my ASP.NET MVC website to export some data as an Excel file. For hours I thought that NPOI was just producing garbage so I switched over to EPPlus. I tested it in LINQPad and it created a proper working XLSX file, so I moved the code over to the MVC app. AGAIN, I get corrupted files. By chance I happened to look at the temp directory and saw that the file created by EPPlus is 3.87KB and works perfectly, but the FileResult is returning a file that's 6.42KB, which is corrupted. Why is this happening? I read somewhere that it was the server GZip compression causing it, so I turned it off, and it had no effect. Someone, please help me, I'm going out of my mind... Here's my code.

[HttpGet]
public FileResult Excel(
    CenturyLinkOrderExcelQueryModel query) {
    var file = Manager.GetExcelFile(query); // FileInfo

    return File(file.FullName, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", query.FileName);
}
Gup3rSuR4c
  • 9,145
  • 10
  • 68
  • 126
  • What is the type and value of file.FullName? Is it a byte array? Have you tried adding the proper file extension like ".xls" to the file name? – John Ephraim Tugado Mar 14 '16 at 05:12
  • `file` is of type `FileInfo`, so no it's not a byte array. The path returned is correct, and with a proper extension. – Gup3rSuR4c Mar 14 '16 at 06:07

2 Answers2

2

As far as I'm concerned there's an issue with the FileResult and it's accompanying methods. I ended up "resolving" the issue by overriding the Response object:

[HttpGet]
public void Excel(
    CenturyLinkOrderExcelQueryModel query) {
    var file = Manager.GetExcelFile(query);

    Response.Clear();
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    Response.AddHeader("Content-Disposition", "attachment; filename=" + query.FileName);
    Response.BinaryWrite(System.IO.File.ReadAllBytes(file.FullName));
    Response.Flush();
    Response.Close();
    Response.End();
}
Gup3rSuR4c
  • 9,145
  • 10
  • 68
  • 126
0

Try using the OpenRead from FileInfo to get a file stream and see if that works.

[HttpGet]
public FileResult Excel(CenturyLinkOrderExcelQueryModel query) {
    var file = Manager.GetExcelFile(query); // FileInfo
    var fileStream = file.OpenRead();
    return File(fileStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", query.FileName);
}
Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • It didn't work. The file it spat out was 6.44KB, .02KB larger than the code I had up there before. Is it possible that it's adding response headers as part of the binary? It's the only information I can think of that would be present with the request. – Gup3rSuR4c Mar 14 '16 at 06:16
  • Just for debugging do a check on the length of `fileStream` and see of it is the expected size of the file. – Nkosi Mar 14 '16 at 06:19
  • I did earlier, and it was the expected size, 3972 (or somewhere there, but it was the ~4KB file I was expecting). – Gup3rSuR4c Mar 14 '16 at 06:23
  • 1
    It was the expected size when it was getting to the `return File(...)` line. Thereafter it is my belief that the framework was padding it somehow with something and sending back a file with an inflated size that couldn't be opened. I posted my answer as to how I "resolved" it below. As far as I'm concerned, this is a framework issue. – Gup3rSuR4c Mar 14 '16 at 06:36