Skip to content

Conversation

MaxxBahr
Copy link

Add blob() method to JS side of Blazor DotNetStreamReference

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

Added method readableStreamToBlob() which returns a Blob from the given stream.

Description

The readableStreamToBlob method is an asynchronous function defined on the internal DotNetStream class. Its purpose is to convert a JavaScript ReadableStream (typically received from .NET interop) into a Blob URL that can be used in browser APIs (such as for downloads or displaying media).

Step-by-step Description

Obtain a Stream Reader:

The function first awaits the result of this.stream(), which returns a ReadableStream. It then calls .getReader() on this stream to obtain a reader for consuming the stream's data chunks.

Read All Chunks:

It initializes an empty array called chunks. Using a while (true) loop, it repeatedly calls reader.read(), which returns a promise resolving to an object with { done, value }.

If done is true, the stream has ended and the loop breaks.
Otherwise, it pushes the value (a chunk of data, typically a [Uint8Array]) into the [chunks] array.

Create a Blob:

After reading all chunks, it creates a new [Blob] from the collected [chunks] array. The mimeType parameter (defaulting to "application/octet-stream") is used as the Blob's type.

Create a Blob URL:

It then calls URL.createObjectURL(blob) to generate a temporary URL representing the Blob's data in memory.

Return the URL:

The function returns this Blob URL as a string. This URL can be used in browser contexts to reference the streamed data (for example, as a download link or image source).

Fixes #47685

@MaxxBahr MaxxBahr requested a review from a team as a code owner June 10, 2025 15:06
@github-actions github-actions bot added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label Jun 10, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jun 10, 2025
@MaxxBahr
Copy link
Author

@dotnet-policy-service agree

@martincostello martincostello added area-blazor Includes: Blazor, Razor Components and removed needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically labels Jun 10, 2025
@pavelsavara
Copy link
Member

@MaxxBahr could the same be achieved from user code ?

The issue speaks about not loading the whole (video) stream into memory at the same time. This PR does load it all.

@TimoP01
Copy link

TimoP01 commented Jun 11, 2025

@pavelsavara : The design details for the blob() method are outlined in the related design proposal #61826.

@dotnet-policy-service dotnet-policy-service bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Jun 20, 2025
Copy link

Commenter does not have sufficient privileges for PR 62298 in repo dotnet/aspnetcore

@MaxxBahr
Copy link
Author

/azp run

Copy link

Commenter does not have sufficient privileges for PR 62298 in repo dotnet/aspnetcore

@javiercn javiercn added the pr: pending author input For automation. Specifically separate from Needs: Author Feedback label Sep 5, 2025
@javiercn
Copy link
Member

javiercn commented Sep 5, 2025

For starters, there need to be E2E tests for this.

*/
export function createJSObjectReference(jsObject: any): any {
if (jsObject && typeof jsObject === "object") {
if (jsObject && (typeof jsObject === "object" || jsObject instanceof Function)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unclear why this change is needed

Comment on lines +768 to +781
async readableStreamToBlob(mimeType: string = "application/octet-stream"): Promise<string> {
const reader = (await this.stream()).getReader();
const chunks: BlobPart[] = [];

while (true) {
const { done, value } = await reader.read();
if (done) break;
chunks.push(value);
}

const blob = new Blob(chunks, {type: mimeType});
const url = URL.createObjectURL(blob);
return url;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not building a blob, but an object URL

@dotnet-policy-service dotnet-policy-service bot added the stale Indicates a stale issue. These issues will be closed automatically soon. label Sep 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-blazor Includes: Blazor, Razor Components community-contribution Indicates that the PR has been added by a community member pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun pr: pending author input For automation. Specifically separate from Needs: Author Feedback stale Indicates a stale issue. These issues will be closed automatically soon.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add blob() method to JS side of Blazor DotNetStreamReference

5 participants