async Task UploadFiles(IReadOnlyList<IBrowserFile> file)
{
foreach (var item in file)
{
try
{
using (var stream = item.OpenReadStream(item.Size))
{
byte[] bytes = new byte[stream.Length];
await stream.ReadAsync(bytes, 0, (int)stream.Length);
files.Add(new FileInfoModel() { FileName = item.Name, ContentType = item.ContentType, Bytes = bytes });
}
}
catch (Exception ex)
{
throw;
}
}
StateHasChanged();
}
{Microsoft.JSInterop.JSException: r.arrayBuffer is not a function
TypeError: r.arrayBuffer is not a function
at https://0.0.0.0/_framework/blazor.webview.js:1:34139
at Object.getJSDataStreamChunk (https://0.0.0.0/_framework/blazor.webview.js:1:34178)
at https://0.0.0.0/_framework/blazor.webview.js:1:3337
at new Promise (<anonymous>)
at beginInvokeJSFromDotNet (https://0.0.0.0/_framework/blazor.webview.js:1:3311)
at External.__callback (https://0.0.0.0/_framework/blazor.webview.js:1:42795)
at MessagePort.<anonymous> (<anonymous>:23:21)
at Microsoft.JSInterop.JSRuntime.<InvokeAsync>d__161[[System.Byte[], System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext() at Microsoft.AspNetCore.Components.PullFromJSDataStream.RequestDataFromJSAsync(Int32 numBytesToRead) at Microsoft.AspNetCore.Components.PullFromJSDataStream.ReadAsync(Memory
1 buffer, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Components.Forms.BrowserFileStream.CopyFileDataIntoBuffer(Memory1 destination, CancellationToken cancellationToken) at Microsoft.AspNetCore.Components.Forms.BrowserFileStream.ReadAsync(Memory
1 buffer, CancellationToken cancellationToken)
来自AI:
根据您提供的代码和错误信息,问题可能与上传文件时的JavaScript交互有关。
使用Blazor时,文件上传通常需要与JavaScript进行交互。错误信息中提到了一个名为"arrayBuffer"的函数找不到,这可能是由于JavaScript交互的问题引起的。
您可以尝试以下方法解决此问题:
stream.ReadAsync方法应该与异步流正确配合使用,建议改用MemoryStream来处理字节数组
using (var stream = item.OpenReadStream(maxAllowedSize: 10 * 1024 * 1024))
using (var memoryStream = new MemoryStream())
{
await stream.CopyToAsync(memoryStream);
files.Add(new FileInfoModel()
{
FileName = item.Name,
ContentType = item.ContentType,
Bytes = memoryStream.ToArray()
});
}