-1

我正在使用托管 ASP.NET Core 的 Blazor Web 程序集。我已将基本 URL 设置为“/”

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

string imageUrl = "https://homepages.cae.wisc.edu/~ece533/images/frymire.png";
var imageBytes = await File.ReadAllBytesAsync(imageUrl);

但是 Blazor Web Assembly 在运行时会出现以下错误

blazor.webassembly.js:1 暴击: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 options) <0x2de83f8 + 0x00258> in :0 at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize,System.IO.FileOptions 选项)<0x35ad778 + 0x0001c>

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

这个问题有什么解决办法吗?

4

2 回答 2

0

使用 HTTP 请求而不是文件:

@inject HttpClient _httpClient

@code {
    private async Task LoadFileAsync()
    {
         string imageUrl = "https://homepages.cae.wisc.edu/~ece533/images/frymire.png";
         using var response = await _httpClient.GetAsync(imageUrl).ConfigureAwait(false);
         var imageBytes = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
    }
}
于 2020-07-08T07:40:46.723 回答
0

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

于 2020-07-08T07:36:44.130 回答