I have a class containing some info a user might want to download after visiting a page. Some initial digging points towards using the JSRuntime interop.
MyClass.cs
public class MyClass
{
public int Count { get; set; }
public string Summary { get; set; }
}
MyClassPage.razor
<button type="button" class="ml-auto p-2 btn btn-primary" onclick="@DownloadFile">Download</button>
...
async Task DownloadFile()
{
var testObj = new SpeciesData();
testObj.Count = 100;
testObj.Summary = "Lorem Ipsum";
var fileName = "testing.json";
var serializedObject = JsonSerializer.Serialize(testObj);
byte[] bytes = Encoding.UTF8.GetBytes(serializedObject);
await JS.InvokeAsync<object>(
"saveAsFile",
fileName,
Convert.ToBase64String(bytes ));
}
How do I get the file to download? Not sure where/how I inject the JS file to the proj (with a function named saveAsFile I assume?)
Similar questions I've referenced: