Khalid Ab
Khalid Ab

Reputation: 195

How to Download xls file in c# blazor

I found this video which shows how to download an xlsx file in my app Download XLSX file Blazor

In nutshell, We use a JS helper which does the job.

//cs file
            iJSRuntime.InvokeAsync<ToSheetConvert>(
                    "saveAsFile",
                    "myfile.xlsx",
                    Convert.ToBase64String(fileContents)
                );


//saveAsFile.js file   
    function saveAsFile(fileName, byteBase64) {
    var link = document.createElement('a');
    link.download = fileName;
    link.href = 'data:application/vnd.openxmlformats-pfficedocument.spreadsheetml.sheet;base64,' + byteBase64;
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);

My problem is, this is not working with old version of excel files like .xls, .cvs I think the problem comes from the link.href which is not correct for those extension files... Also don't know which one to put instead... Need to download xls and csv file in blazor

Here the office error message when i try to open the file downloaded Screen KO xls Here when i forced by just changing the extension in the cs File Screen KO xls csfileChange

Upvotes: 1

Views: 3831

Answers (1)

Umair
Umair

Reputation: 5481

You can replace the href link with the below which should work for all file types:

link.href = "data:application/octet-stream;base64," + bytesBase64;

Upvotes: 2

Related Questions