I need to get back an IJSStreamReference from a FileSystemFileHandle in a Blazor component. Using JSInterop, I pass the File Handle to a function to get back the reference.
In the Component:
await using IJSObjectReference module = await js.InvokeAsync<IJSObjectReference>("import", "./js/fileAccessModule.js");IJSStreamReference streamReference = await module.InvokeAsync<IJSStreamReference>("openFileStream", Handle);In the JS module:
export async function openFileStream(fileHandle) { if (!(fileHandle instanceof FileSystemFileHandle)) { throw new Error("Invalid object supplied as handle."); } const file = await fileHandle.getFile(); const buffer = await file.arrayBuffer(); const array = new Uint8Array(buffer); const reference = DotNet.createJSStreamReference(array); return reference;}This fails when returning with with:
Unhandled exception rendering component: Supplied value is not a typed array or blob.
According to the docs I should be able to create a Stream Reference from an ArrayBuffer or typed Array, from https://learn.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/call-dotnet-from-javascript?view=aspnetcore-9.0#create-javascript-object-and-data-references-to-pass-to-net:
Call
DotNet.createJSStreamReference(streamReference)to construct a JS stream reference so that it can be passed to .NET, wherestreamReferenceis anArrayBuffer,Blob, or any typed array, such asUint8ArrayorFloat32Array, used to create the JS stream reference.