Back to Aspnetcore

Breaking change: Blazor: Byte Array Interop

aspnetcore/breaking-changes/6/byte-array-interop.md

latest1.7 KB
Original Source

Blazor: Byte-array interop

Blazor now supports optimized byte-array interop, which avoids encoding and decoding byte-arrays into Base64 and facilitates a more efficient interop process. This applies to both Blazor Server and Blazor WebAssembly.

Version introduced

ASP.NET Core 6.0

Receive byte array in JavaScript from .NET

Old behavior

typescript
function receivesByteArray(data) {
    // Previously, data was a Base64-encoded string representing the byte array.
}

New behavior

typescript
function receivesByteArray(data) {
    // Data is a Uint8Array (no longer requires processing the Base64 encoding).
}

Reason for change

This change was made to create a more efficient interop mechanism for byte arrays.

Receive byte array in JavaScript from .NET

Consider this .NET interop, where you call into JavaScript passing a byte array:

csharp
var bytes = new byte[] { 1, 5, 7 };
await _jsRuntime.InvokeVoidAsync("receivesByteArray", bytes);

In the preceding code example, you'd treat the incoming parameter in JavaScript as a byte array instead of a Base64-encoded string.

Return byte array from JavaScript to .NET

If .NET expects a byte[], JavaScript should provide a Uint8Array. It's still possible to provide a Base64-encoded array using btoa, however that is less performant.

For example, if you have the following code, then you should provide a Uint8Array from JavaScript that's not Base64-encoded:

csharp
var bytes = await _jsRuntime.InvokeAsync<byte[]>("someJSMethodReturningAByteArray");
<!-- ## Category ASP.NET Core ## Affected APIs Not detectable via API analysis -->