Blazor 从Url访问png图像时的基本Url问题

Blazor 从Url访问png图像时的基本Url问题,blazor,blazor-webassembly,Blazor,Blazor Webassembly,我使用的是一个Blazor web程序集,以ASP.NET为核心托管。 我已将基本URL设置为“/” 我想将图像从URL转换为字节数组 所以我用下面的代码来转换 string imageUrl = "https://homepages.cae.wisc.edu/~ece533/images/frymire.png"; var imageBytes = await File.ReadAllBytesAsync(imageUrl); 但BlazorWeb程序集在运行时出现以下错

我使用的是一个Blazor web程序集,以ASP.NET为核心托管。 我已将基本URL设置为“/”

我想将图像从URL转换为字节数组 所以我用下面的代码来转换

string imageUrl = "https://homepages.cae.wisc.edu/~ece533/images/frymire.png";
var imageBytes = await File.ReadAllBytesAsync(imageUrl);
但BlazorWeb程序集在运行时出现以下错误

blazor.webassembly.js:1 crit:Microsoft.AspNetCore.Components.webassembly.Rendering.WebAssemblyRenderer[100]未处理的异常呈现组件:找不到路径“https:/homepages.cae.wisc.edu/~ece533/images/frymire.png”的一部分。 System.IO.DirectoryNotFoundException:找不到路径“/https:/homepages.cae.wisc.edu/~ece533/images/frymire.png”的一部分。在System.IO.FileStream..ctor中(System.String路径、System.IO.FileMode模式、System.IO.FileAccess访问、System.IO.FileShare共享、System.Int32 bufferSize、System.Boolean匿名、System.IO.FileOptions选项)0在System.IO.FileStream..ctor中(System.String路径、System.IO.FileMode模式、System.IO.FileAccess访问、System.IO.FileShare共享、System.Int32 bufferSize、System.IO.FileOptions选项)位于:0处的System.IO.File.ReadAllByteAsync(System.String路径、System.Threading.CancellationToken CancellationToken)in:0位于SlashCare.Client.Pages.Admin.CategoryComponent.EditCategoryOpenAsync(System.Guid categoryId)[0x0011f]位于D:\Projects\SlashCare\Client\Pages\Admin\Category\CategoryComponent.razor.cs:72位于Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedAsyncCompletion(System.Threading.Tasks.Tasks.Task)位于Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(System.Threading.Tasks.Task taskToHandle)的in:0位于:0

正如我在错误/中看到的,它自动附加到导致此问题的路径


有没有解决此问题的方法?

您需要使用HttpClient从URL而不是文件获取数据。

使用HTTP请求而不是文件:

@injecthttpclient\u HttpClient
@代码{
专用异步任务LoadFileAsync()
{
字符串imageUrl=”https://homepages.cae.wisc.edu/~ece533/images/frymire.png”;
使用var response=await\u httpClient.GetAsync(imageUrl.ConfigureAwait(false);
var imageBytes=await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
}
}