简体   繁体   English

如何将 URL 传入<img>来自 Webapi 的标签以接受 base64 的图像?

[英]How to pass URL into <img> tag from Webapi to accept image of base64?

How to pass URL from Webapi into <img> tag.如何将 URL 从Webapi传递到<img>标签。 I have converted the image into base64 string and inserted into Database as a varbinary .我已将图像转换为base64 string并作为varbinary插入Database

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

You need to add data:image/png;base64 at the start of Webapi endpoint in <img> src and not after.您需要在<img> src 中的 Webapi 端点的开头而不是之后添加data:image/png;base64 See the below code.请参阅下面的代码。

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

Another way to achieve same thing:实现相同目的的另一种方法:

JQuery: JQuery:

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

In HTML:在 HTML 中:

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

Use angularjs ng-src .使用 angularjs ng-src First execute a GET request in your controller and then save the response in a variable to use it with ng-src :首先在 controller 中执行 GET 请求,然后将响应保存在变量中以与ng-src一起使用:

$http.get('https://localhost:44381/api/Account/get-resource-imagedata').then(function(response) {
    $scope.image = response.data;
});

In template:在模板中:

<img data-ng-src="data:image/jpg;base64,{{image}}" alt="img"/>

Here's an example to show base64 images.这是一个显示 base64 图像的示例。

<img src="data:image/jpeg;base64,R0lGODlhPQBEAPeoAJosM//AwO/AwHVYZ/z595kzAP/s7P+goOX"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM