How to download a file from blazor server side

11,146

The solution I ended up using was JS Interop to redirect to the file which then downloaded it.

public async Task DownloadFileAsync(string path)
{
    await Js.InvokeAsync<string>("downloadFile", path);
}

// In JS
function downloadFile(filename) {
    location.href = '/api/downloads/' + filename;
}
Share:
11,146
devlife
Author by

devlife

Developer, entrepreneur, child, adult, learning addict

Updated on June 15, 2022

Comments

  • devlife
    devlife almost 2 years

    I have a server side blazor app which builds up a lot of data and when a user clicks a button will use that data to generate an excel file. All of that is working fine. But my question is what is the appropriate way to download that in-memory file? I know I can save it to the web server disk and do a redirect or something like that to download it I would prefer not to have to save the file to disk if I don't have to.