Last active
January 26, 2022 17:02
-
-
Save juanonsoftware/103fec4fa9c5b9e2ab14 to your computer and use it in GitHub Desktop.
Displaying image from webapi using javascript base64
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WebAPI | |
// GET api/values | |
public dynamic Get() | |
{ | |
var root = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; | |
var path = Path.Combine(root, "App_Data/Koala.jpg"); | |
var bytes = File.ReadAllBytes(path); | |
var base64 = Convert.ToBase64String(bytes); | |
return "data:image/jpeg;base64," + base64; | |
} | |
} | |
JS & HTML | |
<body> | |
<script> | |
//$.ajax({ | |
// url: '//localhost:882/api/values', | |
// type: "GET", | |
// success: function (data) { | |
// //alert(JSON.stringify(data)); | |
// } | |
//}).done(function (data) { | |
// alert(JSON.stringify(data)); | |
//}); | |
$.ajax({ | |
url: '//localhost:882/api/image', | |
type: "GET", | |
success: function (data) { | |
$('#testimg').attr('src', data); | |
$('#testimg').attr('style', 'display: block;'); | |
} | |
}); | |
</script> | |
<img id="testimg" src="#" alt="Get from webapi" style="display: none;" /> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment