本文介绍了如何从blazor服务器端下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务器端的blazor应用程序,该应用程序会构建大量数据,并且当用户单击按钮时,它将使用该数据来生成excel文件.所有这些都工作正常.但是我的问题是下载该内存文件的合适方法是什么?我知道我可以将其保存到Web服务器磁盘上并进行重定向或类似的下载操作,如果不需要,我希望不必将文件保存到磁盘中.

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.

推荐答案

我最终使用的解决方案是JS Interop重定向到该文件,然后下载了该文件.

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;
}

这篇关于如何从blazor服务器端下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 17:04