Blazor Server:将在浏览器本地存储的 Blob 转换为 Base64 字符串

huangapple go评论111阅读模式
英文:

Blazor Server: Convert blob locally stored in browser to Base64 String

问题

我正在Blazor Server Net 6中创建一个应用程序。

通过查阅文档,我成功地将图像保存为本地浏览器上的Blob。

图像在浏览器中渲染得很完美。

Blob URL看起来类似于:

blob:https://localhost:7066/94065f17-5b19-47e4-9256-59f3e237a662

现在,我需要将Blob读取为Base64字符串。我尝试使用IHTTPClient,如下所示:

var result = await httpClient.GetStreamAsync(_card.LogoImageDetails.ActualImage);

但是,它抛出了下面的异常。

等待一个有效的解决方案来解决这个问题。

Saad

英文:

I am creating an application in Blazor Server Net 6

I was able to save the image as a blob on the local browser by going through the documentation

https://learn.microsoft.com/en-us/aspnet/core/blazor/images?view=aspnetcore-7.0#stream-image-data

The images are rendering perfectly in the browser
Blazor Server:将在浏览器本地存储的 Blob 转换为 Base64 字符串

The Blob URL looks something like this

blob:https://localhost:7066/94065f17-5b19-47e4-9256-59f3e237a662

Now, I need to read the blob as a base 64 string. I tried to use IHTTPClient as mentioned below

var result=await httpClient.GetStreamAsync(_card.LogoImageDetails.ActualImage);

But, it throws an exception given below
Blazor Server:将在浏览器本地存储的 Blob 转换为 Base64 字符串

Waiting for an efficient solution to solve this issue

Regards

Saad

答案1

得分: 0

以下是翻译好的部分:

这是解决方案

创建了如下的JS方法:

const blobToBase64 = async blob => {
    return new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.onload = () => resolve(reader.result)
        reader.error = (err) => reject(err)
        reader.readAsDataURL(blob)
    })
}

window.convertBlobURLToBase64 = async (url) => {
    const response = await fetch(url)
    const blob = await response.blob();
    const imageBase64 = await blobToBase64(blob)
    return imageBase64; 
}

使用JSInterop获取Base64字符串

private async Task UploadImagesOnBlobAsync()
{
    var result = await _jsRunTime.InvokeAsync<string>("convertBlobURLToBase64", _card.LogoImageDetails.ActualImage);
}
英文:

Here is the solution

Created JS methods like this:

const blobToBase64 = async blob =&gt; {
    return new Promise((resolve, reject) =&gt; {
        const reader = new FileReader();
        reader.onload = () =&gt; resolve(reader.result)
        reader.error = (err) =&gt; reject(err)
        reader.readAsDataURL(blob)
    })
}

window.convertBlobURLToBase64 = async (url) =&gt; {
    const response = await fetch(url)
    const blob = await response.blob();
    const imageBase64 = await blobToBase64(blob)
    return imageBase64; 
}

Use JSInterop to get the base 64 string

 private async Task UploadImagesOnBlobAsync()
    {
        var result=await _jsRunTime.InvokeAsync&lt;string&gt;(&quot;convertBlobURLToBase64&quot;, _card.LogoImageDetails.ActualImage);
    }

huangapple
  • 本文由 发表于 2023年7月20日 16:53:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76728201.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定