image我有試圖通過 Web API (Net6) 檢索并在 Blazor Web 程式集中顯示的SQL Server型別資料。我可以取回資料字串,但我沒有運氣將其轉換為正確的格式。
Web API 是
public byte[] SQLGetImageBytes(int Id)
{
string SqlConnectionString = configuration.GetValue<string>("SqlConnection");
IDbConnection db = new SqlConnection(SqlConnectionString);
var data = db.QueryFirstOrDefault<Thumbs>(@"SELECT [DocumentID], CAST([Page1] AS VARBINARY(MAX)) As Page1 FROM[Operations_cache].[dbo].[tblLargeThumbs] where[DocumentId] = " Id);
return data.Page1;
}
在 API 中,端點是
app.MapGet("/thumbbytes/{id}", (int id) =>
{
SQLQuery sqlq = new();
byte[] imageData = sqlq.SQLGetImageBytes(id);
string imageDataready = string.Concat("data:image/png;base64,",Convert.ToBase64String(imageData));
return imageDataready;
});
固定部分:我的客戶呼叫 api,回傳的字串包括引號作為字串的一部分:
public string imgstring {get; set;}
protected override async Task OnInitializedAsync()
{
var response = await Http.GetAsync(@"https://example.com:4439/thumbbytes/47992");
string imgresponse = response.Content.ReadAsStringAsync().Result;
string noquotes = imgresponse.Substring(1, imgresponse.Length - 2);
imgstring = "data:image/png;base64, " noquotes;
}
FIXED 剃須刀頁面是(修復是將 imgstring 括在引號中)
@if (imgstring == null)
{}
else
{
<img src="@imgstring" />
}
我在網上找到的大多數“幫助”都參考了在跨平臺 Blazor 中不起作用的 Windows 平臺包(系統繪圖等)。
I was trying to do this in .NET 6 (vs full/classic .NET), and I have been through WAY too many hours of trying different combinations (different types of API responses, bytes/strings/etc, and tried retrieving as png/jpg/bmp - pretty sure it is png), but the best I can get is an image placeholder in Blazor/browser, and a string of characters in Insomnia/api testing. So I hope I am just missing something simple. I'm working read-only with image data that I didn't create (and yes, it is in image format, which I understand is to be deprecated), so I can't just change how it is stored.
uj5u.com熱心網友回復:
在這一行的 API 端點上放置一個斷點,并復制回傳的字串
return imageDataready;
在 Blazor 頁面中創建一個 img 標簽并粘貼復制的字串,看看它是否正在渲染影像
<img src="paste here image data" />
看這段代碼,你在這里創建了一個新的 imgstring 變數
protected override async Task OnInitializedAsync()
{
var response = await Http.GetAsync(@"https://example.com:4439/thumbbytes/47992");
string imgstring = await response.Content.ReadAsStringAsync();
}
而它應該是
@code{
string imgstring
protected override async Task OnInitializedAsync()
{
var response = await Http.GetAsync(@"https://example.com:4439/thumbbytes/47992");
imgstring = await response.Content.ReadAsStringAsync();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/435484.html
