From 982a1bf74325391e9bf3782aa593e23fec9d5f6d Mon Sep 17 00:00:00 2001 From: guardrex <1622880+guardrex@users.noreply.github.com> Date: Mon, 12 Jul 2021 15:51:00 -0500 Subject: [PATCH 1/5] Blazor byte array support --- .../size-limits.md} | 3 + .../call-dotnet-from-javascript.md | 62 ++++++++++++++++++- .../call-javascript-from-dotnet.md | 54 +++++++++++++++- 3 files changed, 117 insertions(+), 2 deletions(-) rename aspnetcore/blazor/includes/{js-interop-size-limits.md => js-interop/size-limits.md} (95%) diff --git a/aspnetcore/blazor/includes/js-interop-size-limits.md b/aspnetcore/blazor/includes/js-interop/size-limits.md similarity index 95% rename from aspnetcore/blazor/includes/js-interop-size-limits.md rename to aspnetcore/blazor/includes/js-interop/size-limits.md index 08801676bb57..a41f2806fccd 100644 --- a/aspnetcore/blazor/includes/js-interop-size-limits.md +++ b/aspnetcore/blazor/includes/js-interop/size-limits.md @@ -1,3 +1,6 @@ +--- +no-loc: [Home, Privacy, Kestrel, appsettings.json, "ASP.NET Core Identity", cookie, Cookie, Blazor, "Blazor Server", "Blazor WebAssembly", "Identity", "Let's Encrypt", Razor, SignalR] +--- *This section only applies to Blazor Server apps. In Blazor WebAssembly, the framework doesn't impose a limit on the size of JavaScript (JS) interop inputs and outputs.* In Blazor Server, JS interop calls are limited in size by the maximum incoming SignalR message size permitted for hub methods, which is enforced by (default: 32 KB). JS to .NET SignalR messages larger than throw an error. The framework doesn't impose a limit on the size of a SignalR message from the hub to a client. diff --git a/aspnetcore/blazor/javascript-interoperability/call-dotnet-from-javascript.md b/aspnetcore/blazor/javascript-interoperability/call-dotnet-from-javascript.md index 0bf86268a0d2..d87efd3b22da 100644 --- a/aspnetcore/blazor/javascript-interoperability/call-dotnet-from-javascript.md +++ b/aspnetcore/blazor/javascript-interoperability/call-dotnet-from-javascript.md @@ -404,9 +404,69 @@ Objects that contain circular references can't be serialized on the client for e * .NET method calls. * JavaScript method calls from C# when the return type has circular references. +::: moniker range=">= aspnetcore-6.0" + +## Byte array support + +Blazor supports optimized byte array JS interop that avoids encoding/decoding byte arrays into Base64. The following example uses JS interop to pass a byte array to .NET. + +Inside the closing `` tag of `wwwroot/index.html` (Blazor WebAssembly) or `Pages/_Host.cshtml` (Blazor Server), provide a `sendByteArray` JS function. The function is called by a button in the component and doesn't return a value: + +```html + +``` + +`Pages/ReceiveByteArray.razor`: + +```razor +@page "/receive-byte-array" +@using System.Text + +

Receive Byte Array (JS to .NET)

+ +

+ +

+ +

+ @result +

+ +

+ Quote ©2005 Universal Pictures: + Serenity
+ Jewel Staite on IMDB +

+ +@code { + private string result; + + [JSInvokable] + public static Task ReceiveBytes(byte[] receivedBytes) + { + return Task.FromResult( + Encoding.Unicode.GetString(receivedBytes, 0, receivedBytes.Length)); + } +} +``` + +For information on using a byte array when calling JavaScript from .NET, see . + +::: moniker-end + ## Size limits on JavaScript interop calls -[!INCLUDE[](~/blazor/includes/js-interop-size-limits.md)] +[!INCLUDE[](~/blazor/includes/js-interop/size-limits.md)] ::: moniker range=">= aspnetcore-5.0" diff --git a/aspnetcore/blazor/javascript-interoperability/call-javascript-from-dotnet.md b/aspnetcore/blazor/javascript-interoperability/call-javascript-from-dotnet.md index 81ea23f3bd46..ee097952a6b5 100644 --- a/aspnetcore/blazor/javascript-interoperability/call-javascript-from-dotnet.md +++ b/aspnetcore/blazor/javascript-interoperability/call-javascript-from-dotnet.md @@ -643,9 +643,61 @@ In the preceding example: ::: moniker-end +::: moniker range=">= aspnetcore-6.0" + +## Byte array support + +Blazor supports optimized byte array JS interop that avoids encoding/decoding byte arrays into Base64. The following example uses JS interop to pass a byte array to Javascript. + +Inside the closing `` tag of `wwwroot/index.html` (Blazor WebAssembly) or `Pages/_Host.cshtml` (Blazor Server), provide a `receiveByteArray` JS function. The function is called with and doesn't return a value: + +```html + +``` + +`Pages/SendByteArray.razor`: + +```razor +@page "/send-byte-array" +@inject IJSRuntime JS + +

Send Byte Array (.NET to JS)

+ +

+ +

+ +

+ Quote ©2005 Universal Pictures: + Serenity
+ Jewel Staite on IMDB +

+ +@code { + private async Task SendBytes() + { + var bytes = new Byte[] { 0x45, 0x76, 0x65, 0x72, 0x79, 0x74, 0x68, 0x69, + 0x6e, 0x67, 0x27, 0x73, 0x20, 0x73, 0x68, 0x69, 0x6e, 0x79, 0x2c, + 0x20, 0x43, 0x61, 0x70, 0x74, 0x69, 0x61, 0x6e, 0x2e, 0x20, 0x4e, + 0x6f, 0x74, 0x20, 0x74, 0x6f, 0x20, 0x66, 0x72, 0x65, 0x74, 0x2e }; + await JS.InvokeVoidAsync("receiveByteArray", bytes); + } +} +``` + +For information on using a byte array when calling .NET from JavaScript, see . + +::: moniker-end + ## Size limits on JavaScript interop calls -[!INCLUDE[](~/blazor/includes/js-interop-size-limits.md)] +[!INCLUDE[](~/blazor/includes/js-interop/size-limits.md)] ::: moniker range=">= aspnetcore-5.0" From a2b8db300126b4c656acabc4516a6b80c524ea3f Mon Sep 17 00:00:00 2001 From: guardrex <1622880+guardrex@users.noreply.github.com> Date: Tue, 13 Jul 2021 10:34:36 -0500 Subject: [PATCH 2/5] Updates --- .../call-dotnet-from-javascript.md | 33 ++++++++----------- .../call-javascript-from-dotnet.md | 27 +++++++++------ 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/aspnetcore/blazor/javascript-interoperability/call-dotnet-from-javascript.md b/aspnetcore/blazor/javascript-interoperability/call-dotnet-from-javascript.md index d87efd3b22da..b4f089dd4764 100644 --- a/aspnetcore/blazor/javascript-interoperability/call-dotnet-from-javascript.md +++ b/aspnetcore/blazor/javascript-interoperability/call-dotnet-from-javascript.md @@ -414,32 +414,29 @@ Inside the closing `` tag of `wwwroot/index.html` (Blazor WebAssembly) or ```html ``` -`Pages/ReceiveByteArray.razor`: +`Pages/SendByteArrayJSToDotNet.razor`: ```razor -@page "/receive-byte-array" +@page "/send-byte-array-js-to-dotnet" @using System.Text -

Receive Byte Array (JS to .NET)

+

Send Byte Array JS to .NET

- -

- -

- @result +

@@ -449,10 +446,8 @@ Inside the closing `` tag of `wwwroot/index.html` (Blazor WebAssembly) or

@code { - private string result; - [JSInvokable] - public static Task ReceiveBytes(byte[] receivedBytes) + public static Task ReceiveByteArray(byte[] receivedBytes) { return Task.FromResult( Encoding.Unicode.GetString(receivedBytes, 0, receivedBytes.Length)); @@ -460,7 +455,7 @@ Inside the closing `` tag of `wwwroot/index.html` (Blazor WebAssembly) or } ``` -For information on using a byte array when calling JavaScript from .NET, see . +For information on using a byte array when calling JavaScript from .NET, see . ::: moniker-end diff --git a/aspnetcore/blazor/javascript-interoperability/call-javascript-from-dotnet.md b/aspnetcore/blazor/javascript-interoperability/call-javascript-from-dotnet.md index ee097952a6b5..58ce565a9ce2 100644 --- a/aspnetcore/blazor/javascript-interoperability/call-javascript-from-dotnet.md +++ b/aspnetcore/blazor/javascript-interoperability/call-javascript-from-dotnet.md @@ -654,25 +654,29 @@ Inside the closing `` tag of `wwwroot/index.html` (Blazor WebAssembly) or ```html ``` -`Pages/SendByteArray.razor`: +`Pages/SendByteArrayDotNetToJS.razor`: ```razor -@page "/send-byte-array" +@page "/send-byte-array-dotnet-to-js" @inject IJSRuntime JS -

Send Byte Array (.NET to JS)

+

Send Byte Array .NET to JS

+

+ @result +

+

Quote ©2005 Universal Pictures: Serenity
@@ -680,13 +684,16 @@ Inside the closing `` tag of `wwwroot/index.html` (Blazor WebAssembly) or

@code { + private string result; + private async Task SendBytes() { - var bytes = new Byte[] { 0x45, 0x76, 0x65, 0x72, 0x79, 0x74, 0x68, 0x69, - 0x6e, 0x67, 0x27, 0x73, 0x20, 0x73, 0x68, 0x69, 0x6e, 0x79, 0x2c, - 0x20, 0x43, 0x61, 0x70, 0x74, 0x69, 0x61, 0x6e, 0x2e, 0x20, 0x4e, + var bytes = new byte[] { 0x45, 0x76, 0x65, 0x72, 0x79, 0x74, 0x68, 0x69, + 0x6e, 0x67, 0x27, 0x73, 0x20, 0x73, 0x68, 0x69, 0x6e, 0x79, 0x2c, + 0x20, 0x43, 0x61, 0x70, 0x74, 0x69, 0x61, 0x6e, 0x2e, 0x20, 0x4e, 0x6f, 0x74, 0x20, 0x74, 0x6f, 0x20, 0x66, 0x72, 0x65, 0x74, 0x2e }; - await JS.InvokeVoidAsync("receiveByteArray", bytes); + + result = await JS.InvokeAsync("receiveByteArray", bytes); } } ``` From 231e20ff1ec8f42b429612e7ed7d788deb952641 Mon Sep 17 00:00:00 2001 From: guardrex <1622880+guardrex@users.noreply.github.com> Date: Wed, 14 Jul 2021 03:46:10 -0500 Subject: [PATCH 3/5] Updates --- .../call-dotnet-from-javascript.md | 6 +++--- .../call-javascript-from-dotnet.md | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/aspnetcore/blazor/javascript-interoperability/call-dotnet-from-javascript.md b/aspnetcore/blazor/javascript-interoperability/call-dotnet-from-javascript.md index b4f089dd4764..e1f4fab86614 100644 --- a/aspnetcore/blazor/javascript-interoperability/call-dotnet-from-javascript.md +++ b/aspnetcore/blazor/javascript-interoperability/call-dotnet-from-javascript.md @@ -414,12 +414,12 @@ Inside the closing `` tag of `wwwroot/index.html` (Blazor WebAssembly) or ```html ``` -`Pages/SendByteArrayJSToDotNet.razor`: +`Pages/CallDotNetExample7.razor`: ```razor -@page "/send-byte-array-js-to-dotnet" +@page "/call-dotnet-example-7" @using System.Text -

Send Byte Array JS to .NET

+

Call .NET Example 7

diff --git a/aspnetcore/blazor/javascript-interoperability/call-javascript-from-dotnet.md b/aspnetcore/blazor/javascript-interoperability/call-javascript-from-dotnet.md index 2d3a524db7c4..cd2474d0a26c 100644 --- a/aspnetcore/blazor/javascript-interoperability/call-javascript-from-dotnet.md +++ b/aspnetcore/blazor/javascript-interoperability/call-javascript-from-dotnet.md @@ -661,13 +661,13 @@ Inside the closing `` tag of `wwwroot/index.html` (Blazor WebAssembly) or ``` -`Pages/SendByteArrayDotNetToJS.razor`: +`Pages/CallJsExample9.razor`: ```razor -@page "/send-byte-array-dotnet-to-js" +@page "/call-js-example-9" @inject IJSRuntime JS -

Send Byte Array .NET to JS

+

Call JS Example 9

@@ -756,9 +756,9 @@ Place the following `