Skip to content

Commit bf6b4a2

Browse files
Added Sample with Streaming Large Blob
1 parent fb1164a commit bf6b4a2

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
@page "/StreamLargeBlob"
2+
@inject IJSRuntime JSRuntime
3+
4+
<PageTitle>FileAPI - Stream Large Blob</PageTitle>
5+
6+
<h2>Streaming a large Blob</h2>
7+
A common problem that people face when working with files is that they can be very big.
8+
This is especially a problem if you want to read them in Blazor Server where there is a limit on the size of messages that can be sent over SignalR.
9+
<br />
10+
This sample will illustrate stream reading a large Blob to .NET using the inbuilt <code>stream()</code> method available on a <code>Blob</code>.
11+
<br />
12+
<br />
13+
<ol>
14+
<li>We have created a method in JS called <code>hugeBlob()</code> which creates a huge object in the form of a large empty array, serialized to a JSON string, and then parsed into the <code>Blob</code> constructor.</li>
15+
<li>We construct a wrapper around the JS object reference <i>(jSBlob)</i> by calling <code>Blob.Create(JSRuntime, jSBlob)</code>.</li>
16+
<li>We get a <code>ReadableStream</code> from the Blob by calling <coded>await blob.StreamAsync()</coded>.</li>
17+
<li>We get the default reader of the stream by calling <code>await stream.GetDefaultReaderAsync()</code>.</li>
18+
<li>We iterate the chunks of the reader and append the chunk to our result. Below you can see chunk sizes read and the length of the result that has been built.</li>
19+
</ol>
20+
21+
<b>Last Read Lengths:</b> @string.Join(", ", readLengths)
22+
<br />
23+
<b>Accumulatively Result Length:</b> @result.Length characters
24+
25+
@code {
26+
private string result = "";
27+
private List<int> readLengths = new();
28+
29+
protected override async Task OnAfterRenderAsync(bool firstRender)
30+
{
31+
if (!firstRender) return;
32+
33+
var jSBlob = await JSRuntime.InvokeAsync<IJSObjectReference>("hugeBlob");
34+
35+
var blob = Blob.Create(JSRuntime, jSBlob);
36+
37+
var stream = await blob.StreamAsync();
38+
39+
var reader = await stream.GetDefaultReaderAsync();
40+
41+
await foreach (var chunk in reader.IterateStringsAsync())
42+
{
43+
if (chunk is null) break;
44+
readLengths.Add(chunk.Length);
45+
result += chunk;
46+
StateHasChanged();
47+
await Task.Delay(100);
48+
}
49+
}
50+
}

samples/KristofferStrube.Blazor.FileAPI.WasmExample/Shared/NavMenu.razor

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
<span class="oi oi-arrow-bottom" aria-hidden="true"></span> FileReader
2525
</NavLink>
2626
</div>
27+
<div class="nav-item px-3">
28+
<NavLink class="nav-link" href="StreamLargeBlob">
29+
<span class="oi oi-hard-drive" aria-hidden="true"></span> Stream Large Blob
30+
</NavLink>
31+
</div>
2732
<div class="nav-item px-3">
2833
<NavLink class="nav-link" href="Status">
2934
<span class="oi oi-warning" aria-hidden="true"></span> API Coverage Status

samples/KristofferStrube.Blazor.FileAPI.WasmExample/wwwroot/index.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@
4141
}
4242
</script>
4343

44+
<script>
45+
function hugeBlob() { return new Blob([JSON.stringify(new Array(10_000_000))]); }
46+
47+
function getAttribute(object, attribute) { return object[attribute]; }
48+
</script>
49+
4450
<link href="css/bootstrap/bootstrap.min.css" rel="stylesheet" />
4551
<link href="css/app.css" rel="stylesheet" />
4652
<link rel="icon" type="image/png" href="favicon.png" />

0 commit comments

Comments
 (0)