Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to download an Excel file through the browser but when I open this file, I get the error in Excel that the file is corrupt.
If I save this file through the backend locally to a directory on my computer, I can open the file and see data in it with no issues. The corruption issue appears only when I download the file through the browser.

Below is what I have tried so far but still didn't manage to get it fixed.

Can someone help regarding this issue?
Thank you.

EDIT: This is a screenshot of the result of the "console.log(data)", maybe it will provide an extra clue about what is going wrong.

What I have tried:

For testing purposes and trying to fix the issue, I am just generating an empty Excel file based on a template in the backend, then passing it to the frontend.

File1.cs
C#
using var workBook = XLWorkbook.OpenFromTemplate(templatePath);
`
`
workBook.SaveAs(memoryStream);

return memoryStream;
//In this class, I tried saving the file to a local directory, 
//and it works with no problem by using workBook.SaveAs(path here, etc.).
}


Controller.cs
C#
 [HttpGet]
public async Task<IActionResult> SaveAction([FromQuery] int? propertyId)
{
`
`
stream.Position = 0;
stream.Flush();// tried without these 2 lines
return File(stream.ToArray(), "text/xls"); // I have tried this contentType // and this: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet".
}


frontendFile.js
This is the js file. I also tried both contentTypes here, but neither changed the end result and file corruption issue.
JavaScript
$.get(_options.saveCoversUrl, function (data) {
                    console.log("file to frontend sent2");
                    console.log(data);
                    var a = document.createElement('a');
                    data = new Blob([data], { type: "text/xls" });
                    var url = window.URL.createObjectURL(data);
                    a.href = url;
                    a.download = 'sample.xlsx';
                    a.click();
                    window.URL.revokeObjectURL(url);
Posted
Updated 24-Oct-23 10:59am
v4
Comments
Richard MacCutchan 19-Oct-23 10:21am    
You should try the application/vnd.ms-excel content type.
xTMx9 19-Oct-23 10:45am    
Unfortunately, this didn't fix the issue.
Richard MacCutchan 19-Oct-23 11:23am    
Sorry, but that's the only suggestion I have. You could try a Google search to see how other people do it.
Member 15627495 19-Oct-23 11:45am    
ensure you have the file on the server , after the uploading step.
you make it too in an hurry.
xTMx9 20-Oct-23 8:38am    
I can't save the file on the server itself, only download the file directly on the user's browser.

1 solution

The output of console.log(data) look legit. It starts with PK and is followed by binary data, which means it looks like a complete ZIP file (which is what *.xlsx are). I suggest reviewing the use of the Blob object by following the example here: Blob - Web APIs | MDN[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900