Skip to content

Commit ad5561a

Browse files
Added Support for Slice and SliceAsync.
1 parent 10f6c26 commit ad5561a

7 files changed

Lines changed: 74 additions & 6 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
@page "/Slice"
2+
@inject IJSRuntime JSRuntime
3+
4+
<PageTitle>FileAPI - Slice</PageTitle>
5+
6+
<h2>Slicing a Blob</h2>
7+
Here we can see how we can slice a <code>Blob</code>. We construct a <code>Blob</code> with the numbers from 0 to 49 encoded as bytes.
8+
<br />
9+
<div>
10+
<b>Slice(20, 30):</b> @slice20_30
11+
</div>
12+
<div>
13+
<b>SliceAsync(end: 20):</b> @slice_end_10
14+
</div>
15+
<div>
16+
<b>SliceAsync(start: 40):</b> @slice_start_40
17+
</div>
18+
<div>
19+
<b>SliceAsync(10, 20, "text/csv"):</b> @slice_start_40 with content type @slice10_20ContentType
20+
</div>
21+
22+
23+
@code {
24+
string slice20_30 = "";
25+
string slice_end_10 = "";
26+
string slice_start_40 = "";
27+
string slice10_20 = "";
28+
string slice10_20ContentType = "";
29+
30+
protected override async Task OnInitializedAsync()
31+
{
32+
var blob = await BlobInProcess.CreateAsync(
33+
JSRuntime,
34+
blobParts: new BlobPart[] { new(Enumerable.Range(0, 50).Select(i => (byte)i).ToArray()) },
35+
options: new() { Type = "text/plain" }
36+
);
37+
38+
slice20_30 = string.Join(",", (await (blob.Slice(20, 30)).ArrayBufferAsync()).Select(b => b.ToString()));
39+
slice_end_10 = string.Join(",", (await (await blob.SliceAsync(end: 10)).ArrayBufferAsync()).Select(b => b.ToString()));
40+
slice_start_40 = string.Join(",", (await (await blob.SliceAsync(start: 40)).ArrayBufferAsync()).Select(b => b.ToString()));
41+
42+
var slice = await blob.SliceAsync(10, 20, "text/csv");
43+
slice10_20 = string.Join(",", (await slice.ArrayBufferAsync()).Select(b => b.ToString()));
44+
slice10_20ContentType = await slice.GetTypeAsync();
45+
}
46+
}

samples/KristofferStrube.Blazor.FileAPI.WasmExample/Pages/Status.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
StateHasChanged();
2424
}
2525

26-
private (int start, int end)[] supportedRows = new (int start, int end)[] { (0, 7), (12, 39), (47, 64), (77, 77), (90, 94) };
26+
private (int start, int end)[] supportedRows = new (int start, int end)[] { (0, 39), (47, 64), (77, 77), (90, 94) };
2727

2828
private const string webIDL = @"[Exposed=(Window,Worker), Serializable]
2929
interface Blob {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
<span class="oi oi-home" aria-hidden="true"></span> Home
1515
</NavLink>
1616
</div>
17+
<div class="nav-item px-3">
18+
<NavLink class="nav-link" href="Slice">
19+
<span class="oi oi-crop" aria-hidden="true"></span> Slice
20+
</NavLink>
21+
</div>
1722
<div class="nav-item px-3">
1823
<NavLink class="nav-link" href="Status">
1924
<span class="oi oi-warning" aria-hidden="true"></span> API Coverage Status

src/KristofferStrube.Blazor.FileAPI/Blob.InProcess.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.JSInterop;
1+
using KristofferStrube.Blazor.Streams;
2+
using Microsoft.JSInterop;
23

34
namespace KristofferStrube.Blazor.FileAPI;
45

@@ -66,4 +67,12 @@ internal BlobInProcess(IJSRuntime jSRuntime, IJSInProcessObjectReference inProce
6667
/// </summary>
6768
/// <returns>The MIME type of this blob.</returns>
6869
public string Type => inProcessHelper.Invoke<string>("getAttribute", JSReference, "type");
70+
71+
public Blob Slice(long? start = null, long? end = null, string? contentType = null)
72+
{
73+
start ??= 0;
74+
end ??= (long)Size;
75+
IJSObjectReference jSInstance = JSReference.Invoke<IJSObjectReference>("slice", start, end, contentType);
76+
return new Blob(jSRuntime, jSInstance);
77+
}
6978
}

src/KristofferStrube.Blazor.FileAPI/Blob.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ public async Task<string> GetTypeAsync()
6767
return await helper.InvokeAsync<string>("getAttribute", JSReference, "type");
6868
}
6969

70+
public async Task<Blob> SliceAsync(long? start = null, long? end = null, string? contentType = null)
71+
{
72+
start ??= 0;
73+
end ??= (long)await GetSizeAsync();
74+
IJSObjectReference jSInstance = await JSReference.InvokeAsync<IJSObjectReference>("slice", start, end, contentType);
75+
return new Blob(jSRuntime, jSInstance);
76+
}
77+
7078
public async Task<ReadableStream> StreamAsync()
7179
{
7280
IJSObjectReference jSInstance = await JSReference.InvokeAsync<IJSObjectReference>("stream");
@@ -94,7 +102,8 @@ public async Task<string> TextAsync()
94102
/// <returns>All bytes in the blob.</returns>
95103
public async Task<byte[]> ArrayBufferAsync()
96104
{
105+
IJSInProcessObjectReference jSInstance = await JSReference.InvokeAsync<IJSInProcessObjectReference>("arrayBuffer");
97106
IJSObjectReference helper = await helperTask.Value;
98-
return await helper.InvokeAsync<byte[]>("arrayBuffer", JSReference);
107+
return await helper.InvokeAsync<byte[]>("arrayBuffer", jSInstance);
99108
}
100109
}

src/KristofferStrube.Blazor.FileAPI/KristofferStrube.Blazor.FileAPI.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
</ItemGroup>
2424

2525
<ItemGroup>
26-
<PackageReference Include="KristofferStrube.Blazor.Streams" Version="0.1.0" />
26+
<PackageReference Include="KristofferStrube.Blazor.Streams" Version="0.2.*" />
2727
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="6.0.*" />
2828
</ItemGroup>
2929

src/KristofferStrube.Blazor.FileAPI/wwwroot/KristofferStrube.Blazor.FileAPI.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
export function getAttribute(object, attribute) { return object[attribute]; }
22

3-
export async function arrayBuffer(blob) {
4-
var buffer = await blob.arrayBuffer();
3+
export async function arrayBuffer(buffer) {
54
var bytes = new Uint8Array(buffer);
65
return bytes;
76
}

0 commit comments

Comments
 (0)