Add a blob([mimeType: string]) method to DotNetStreamReference in Blazor, allowing developers to obtain a Blob from a .NET stream in JavaScript, without fully loading the stream into memory. We chose this specific issue, because it is a "good-first-issue" and we are participating in a germany wide competition, where the subject is to work on issues in open source projects.
Link to issue: https://github.com/dotnet/aspnetcore/issues/47685
Blazor developers can currently pass .NET streams to JavaScript using DotNetStreamReference, which exposes two main methods:
stream() (returns a ReadableStream)arrayBuffer() (loads the full stream into memory)The problem:
<video> elements, file downloads or drag-and-drop functionality).arrayBuffer() is not suitable for large files (like videos) because it fully buffers the stream in memory before use.stream() gives a ReadableStream, but many APIs (like <video src=...>, URL.createObjectURL()) require a Blob, not a stream, and converting a ReadableStream into a Blob in JS is non-trivial and inefficient.Adding a blob() method would:
blob([mimeType: string]) on DotNetStreamReference that returns a Blob representing the .NET stream.<video>, <audio>, URL.createObjectURL()).Browser behavior: While Blobs are widely supported, the implementation detail of streaming data into a Blob without full buffering might depend on browser internals. Some browsers may still buffer the entire Blob in memory before making it available.
Memory use: Even if we avoid buffering in .NET or JS, the underlying browser may still end up consuming memory when the Blob is accessed, especially for very large files.
Implementation complexity: Creating a Blob from a .NET stream across JS interop might require platform-level work to ensure true streaming behavior rather than just buffering and converting.
<video @ref="videoPlayer" controls autoplay></video>
<button @onclick="PlayVideo">Play Streamed Video</button>
@code {
private ElementReference videoPlayer;
private async Task PlayVideo()
{
using var fileStream = File.OpenRead("bigvideo.mp4");
var streamRef = new DotNetStreamReference(fileStream);
// JS call to create a blob URL from the .NET stream
var videoUrl = await JS.InvokeAsync<string>("createVideoBlobUrl", streamRef);
// Set the video source
await JS.InvokeVoidAsync("setVideoSrc", videoPlayer, videoUrl);
}
}
window.createVideoBlobUrl = async function (streamRef) {
const blob = await streamRef.blob("video/mp4");
return URL.createObjectURL(blob);
};
window.setVideoSrc = function (videoElement, src) {
videoElement.src = src;
};