如何使用 Blazor 生成和保存文件客户端? [英] How can one generate and save a file client side using Blazor?

查看:49
本文介绍了如何使用 Blazor 生成和保存文件客户端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个 SPA 来完成客户端的所有工作,甚至生成一些图表/视觉效果.

I want to have a SPA that's doing all the work client side and even generating some graphs/visuals.

我希望能够让用户单击按钮并保存页面中的视觉效果、表格和其他内容(既可见又不可见,因此右键单击保存或复制/粘贴并不总是选项).

I'd like to be able to have the user click buttons and save the visuals, tables, and other things from the page (that are both seen and not seen, so right click save or copy/paste is not always an option).

如何从 webassembly/blazor 库中调用函数、获取其结果并将其保存为客户端的文件?

How do I call a function from the webassembly/blazor library, get it's results and save it as a file on the client side?

这个想法是这样的......?

the idea is something like this...?

cshtml

<input type="file" onchange="@ReadFile">

<input type="file" onchange="@SaveFile">

@functions{
object blazorObject = new blazorLibrary.SomeObject();

void ReadFile(){
    blazorObject.someFunction(...selectedFile?...);

}
void SaveFile(){
    saveFile(...selectedFile..?)
}

}

推荐答案

  1. 添加链接

<a class="form-control btn btn-primary" href="/download?name=test.txt" target="_blank">Download</a>

  1. 添加带有路由的 Razor 页面
    2.1.创建 Razor 页面Download.cshtml"或其他名称...PewPew.cshtml"...无关紧要
    2.2.将下一个代码放在创建的页面中
    @page "/download"
    @model MyNamespace.DownloadModel
  2. 编辑Download.cshtml.cs文件
  1. Add Razor Page with a route
    2.1. Create Razor page 'Download.cshtml' or another name... 'PewPew.cshtml'... does not matter
    2.2. Put the next code in the created page
    @page "/download"
    @model MyNamespace.DownloadModel
  2. Edit Download.cshtml.cs file

public class DownloadModel : PageModel
{
    public IActionResult OnGet(string name) {
        // do your magic here
        var content = new byte[] { 1, 2, 3 };
        return File(content, "application/octet-stream", name);
    }
}

这篇关于如何使用 Blazor 生成和保存文件客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆