How to pass URL from Webapi
into <img>
tag. I have converted the image into base64 string
and inserted into Database
as a varbinary
.
<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. 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:
$.ajax({
url: 'https://localhost:44381/api/Account/get-resource-imagedata',
type: "GET",
success: function (data) {
$('#imgImage').attr('src', data);
}
});
In HTML:
<img id="imgImage" src="#" alt="img"/>
Use 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
:
$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.
<img src="data:image/jpeg;base64,R0lGODlhPQBEAPeoAJosM//AwO/AwHVYZ/z595kzAP/s7P+goOX"/>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.