Blazor Component wrapper for FileSaver.js
- Add Nuget BlazorFileSaver
- Add
<script src="_content/BlazorFileSaver/BlazorFileSaver.min.js"></script>
to the index.html or _Hosts.cshtml - Add AddBlazorFileSaver() to the ConfigureServices in Program.cs as noted below
namespace BlazorFileSaver.Sample
{
public class Program
{
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("app");
builder.Services.AddBlazorFileSaver(); // Add This!
await builder.Build().RunAsync();
}
}
}
@page "/sample"
<h1>BlazorFileSaver.Sample</h1>
<input type="text" @bind="FileName" />
<buton @onclick="Save" class="btn btn-primary">Save</buton>
<textarea class="form-control" type="text" @bind="Data" rows="20"></textarea>
@code {
[Inject]
private IBlazorFileSaver BlazorFileSaver { get; set; }
private string FileName { get; set; } = "Sample.txt";
private string Data { get; set; } = "Sample Text";
private async Task Save()
{
await BlazorFileSaver.SaveAs(FileName, Data);
}
}