Closed
Description
Background and Motivation
Support sending data streams from .NET to JS. This is the other end of the JS to .NET streaming functionality implemented earlier.
Proposed API
namespace Microsoft.JSInterop
{
/// <summary>
/// Represents the reference to a .NET stream sent to JavaScript.
/// </summary>
public sealed class DotNetStreamReference : IDisposable
{
/// <summary>
/// Create a reference to a .NET stream sent to JavaScript.
/// </summary>
/// <param name="stream">The stream being sent to JavaScript.</param>
/// <param name="leaveOpen">A flag that indicates whether the stream should be left open after transmission.</param>
public DotNetStreamReference(Stream stream, bool leaveOpen = false);
/// <summary>
/// The stream being sent to JavaScript.
/// </summary>
public Stream Stream { get; }
/// <summary>
/// A flag that indicates whether the stream should be left open after transmission.
/// </summary>
public bool LeaveOpen { get; }
}
}
Usage Examples
using var data = new System.IO.MemoryStream(new byte[100 * 1024]);
using var streamRef = new DotNetStreamReference(stream: data, leaveOpen: false);
await JS.InvokeVoidAsync("consumeStream", streamRef);
async function consumeStream(streamRef) {
const data = await streamRef.arrayBuffer(); // ArrayBuffer
// or
const stream = await streamRef.stream(); // ReadableStream
}
or as return value:
async function getFileData() {
const streamRef = await DotNet.invokeMethodAsync('BlazorServerApp', 'GetFileData');
}
Alternative Designs
N/A
Risks
N/A
PR: #34817