首页 > 解决方案 > 如何将 URL从 Webapi 传递到标签以接受 base64 的图像?

问题描述

如何将 URL 传递Webapi<img>标签中。我已将图像base64 string转换Databasevarbinary.

<img src="https://localhost:44381/api/Account/get-resource-image;data:image/jpg;base64" alt="img"/>

标签: c#htmlangularjsasp.net-web-apibase64

解决方案


您需要data:image/png;base64在 src 中的 Webapi 端点的开头<img>而不是之后添加。请参阅下面的代码。

<img src="data:image/jpg;base64,https://localhost:44381/api/Account/get-resource-imagedata" alt="img"/>

实现相同目的的另一种方法:

查询:

$.ajax({
        url: 'https://localhost:44381/api/Account/get-resource-imagedata',
        type: "GET",
        success: function (data) {
            $('#imgImage').attr('src', data);
        }
});

在 HTML 中:

<img id="imgImage" src="#" alt="img"/>


推荐阅读