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

查看:570
本文介绍了如何使用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. 使用路线添加剃须刀页面
    2.1.创建剃刀页面" 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天全站免登陆