Skip to content

.NET to JS Streaming Interop API Proposal #35009

Closed
@TanayParikh

Description

@TanayParikh

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

Metadata

Metadata

Assignees

Labels

DoneThis issue has been fixedapi-approvedAPI was approved in API review, it can be implementedarea-blazorIncludes: Blazor, Razor ComponentsenhancementThis issue represents an ask for new feature or an enhancement to an existing one

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions