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.

11
  • 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. Commented Aug 17, 2017 at 23:24
  • 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. Commented Aug 17, 2017 at 23:49
  • 1
    Thanks! Think I got it from here Commented Aug 18, 2017 at 0:03
  • 1
    Thanks, but no luck using those types, still says its corrupt. Commented Aug 21, 2017 at 18:24
  • 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. Commented Aug 22, 2017 at 17:21

1 Answer 1

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)  
    );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.