8

enter image description hereI generated an excel file with OpenXml 2.7.2 in .NETCoreApp 1.1 and saved it into a project folder. Then in my function I read the bytes and try to return it as a File. I don't get an error, but in the response I just get back the binary. So it seems to work but doesn't get downloaded.

Here is my code:

[HttpGet]
[Route("export")]
public IActionResult Export()
    {
        return File(System.IO.File.ReadAllBytes(filePath),
        contentType: "application/octet-stream",
        fileDownloadName: "MySheet.xlsx");
        
    }

If anyone knows how to serve a downloadable excel file in .net core let me know. Any help is appreciated thanks!

UPDATE

I'm not sure the corruption is related to generating the excel file with OpenXml because even when I try to export an empty file that wasn't generated with OpenXml I get the same error message saying "The file is corrupt and cannot be opened". Its also not entirely related to Excel 2016 because I can open other excel files with it fine.

Community
  • 1
  • 1
jonv562
  • 293
  • 1
  • 4
  • 23
  • The code you have should work. I am having trouble understanding what you problem is. The downloaded file will be delivered as binary? What is that image in the question suppose to represent and what tool is that being used? How is the action being called? – Nkosi Aug 17 '17 at 23:04
  • 1
    The image shows what I get back in the response of my http call, which is the excel binary. I don't want that. I want the file to save onto a pc/mac under the name MySheet.xlsx. The tool i used to see the response was Chromes Inspect tool. I'm calling the export route from my front end which is in angular 2. – jonv562 Aug 17 '17 at 23:24
  • If you just enter the url in the browser does it prompt you to download the file, or at least start downloading it if the browser is configured to do so. – Nkosi Aug 17 '17 at 23:28
  • Inspect the raw response and you should see that all the information relating to the file would be included along with the content. The server side code is accurate. It looks like you need to work on how the client side handles the response – Nkosi Aug 17 '17 at 23:30
  • 1
    So I tested the route in Swagger Ui and it does download. The problem was that I had a proxy the points from one port to another so it wasn't downloading but when I dont use the proxy it works. The file comes back corrupted though so still need to do some tweaking. – jonv562 Aug 17 '17 at 23:49
  • At least it is confirmed that the download is working. Review how the file is generated to address the file corruption. – Nkosi Aug 17 '17 at 23:51
  • 1
    Thanks! Think I got it from here – jonv562 Aug 18 '17 at 00:03
  • try `application/vnd.ms-excel.12` or `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet` (official MIME types for .xlsx) instead of `application/octet-stream` – Simon Mourier Aug 18 '17 at 09:40
  • 1
    Thanks, but no luck using those types, still says its corrupt. – jonv562 Aug 21 '17 at 18:24
  • @jonv562 I thought you figured this out already. Anyways...when the file is generated are you able to go to the saved location and open the file? if it is corrupted even then, then that means that there is an issue with the generation of the file. – Nkosi Aug 22 '17 at 11:11
  • 1
    The saved file is fine. When I tested it with a excel file that I didn't generate with OpenXml, it also says it can't open because its corrupted. So it has to be something else besides the way I'm generating the file. – jonv562 Aug 22 '17 at 17:21

1 Answers1

2

I found a work around, so that I don't have to rely on reading the file and serving it which seems to be my issue. Now what I do is i generate the excel and return the url of the excel file. Then in the success response I call window.open().

this.export().subscribe(
        data => window.open(urlToExcelFile, "_blank"),
        err => console.log(err)  
    );
jonv562
  • 293
  • 1
  • 4
  • 23