Blazor到Javascript字节数组互操作
本教程将介绍Blazor到Javascript字节数组互操作的处理方法,这篇教程是从别的地方看到的,然后加了一些国外程序员的疑问与解答,希望能对你有所帮助,好了,下面开始学习吧。
问题描述
我正在尝试将byte
数组从Blazor客户端传递到javascript函数:
private async void ShowImage()
{
SelectedImageBytes = await GetImageData();
if (SelectedImageBytes.Any())
{
ReceivedDataLength = SelectedImageBytes.Length;
//ReceivedDataLength is 131072, which is correct
JS.InvokeVoidAsync("JS.setImage", SelectedImageBytes, 256, 256);
}
StateHasChanged();
}
在Javascript端:
function setImage(data, width, height)
{
console.log("On Javascript I have received an array of " + data.length);
//data.length is 174764
console.log(data);
//...
}
console.log(data)
输出以下内容:
对我来说,它似乎是我的二进制数据的Base64字符串表示。根据维基百科的说法,从字节数组到base64字符串表示,大小大约增加了33%,本例也是如此:131072*1.33~174764
我的问题是:
怎么将字节数组从Blazor(C#)传递和接收到Javascript,而不将其转换为字符串
如果无法执行上述操作,则在Javascript端将Base64字符串转换为字节数组的最佳方式是什么。
推荐答案
我又试了一次:
C#
public void CallJsUnMarshalled()
{
var unmarshalledRuntime = (IJSUnmarshalledRuntime)JS;
unmarshalledRuntime.InvokeUnmarshalled<byte[], int>("JsFunctions.MyFunctionUnmarshalled", MyBytes);
}
Javascript:
function MyFunctionUnmarshalled(bytes)
{
const dataPtr = Blazor.platform.getArrayEntryPtr(bytes, 0, 4);
const length = Blazor.platform.getArrayLength(bytes);
var shorts = new Int16Array(Module.HEAPU8.buffer, dataPtr, length);
return 0;
}
InvokeUnmarshalled
需要返回,因此模板参数中有int
。相反,可能必须返回对该对象的引用来释放它。如果有人能对此发表评论,我将不胜感激(当字节数组不再使用时,javascript是否会释放该内存?)。
第一次测试显示改善了50倍!
好了关于Blazor到Javascript字节数组互操作的教程就到这里就结束了,希望趣模板源码网找到的这篇技术文章能帮助到大家,更多技术教程可以在站内搜索。