I have a web page using angularjs 1.4.8 where I am attempting to display an image obtained from a GET url request.
Below is the code snippet from my page:
<div ng-show="message.message == null && message.is_image != null">
<a href="#" ng-click="downloadFile(message.id_message)">
<img data-ng-src="data:image/{{message.image_resolution}};base64,{{message.image_preview}}"/>
</a>
</div>
The JSON data from Cassandra DB looks like this:
created_date:"2017-03-31 22:05:42.284Z"
id_message:"e6e2a5cb-ec25-472f-a59b-3f16a3a8afa9"
id_user_link:"47ed65bf-5520-4901-88c8-01980ffbcd4d"
id_user_sent:"3495c2de-c93c-4323-8e48-1fcecbfde625"
image_length:174443
image_name:"5.png"
image_preview:"0x89504e470d0a1a0a0000000d49484452000007800000039a080600000079a04f28000038714944415478daecd9496e55570045d13bfff124d442c654016320c4d4219832046308a132087199c26ba4f1fed65ad29ec0e99e71ec97635392244992244992244992b4f90d23489224499
...
... some other 90 lines of symbols
...
00000108401d8006c0096244906600000000008c2006c0036004b922403300000000004610036001b802549920118000000008230001b800dc09224c9000c000000004118800dc00660499264000600000080200cc0066003b024493200030000004010066003b001589224198001000000200803b001d8002c49920cc000000000108401d8006c0096244906600000000008c2006c0036004b92a4ff95fe0ffc7d46dd1b63a2b10000000049454e44ae426082"
image_resolution:"png"
is_image:1
message:null
Unfortunately, the images are not displaying on the webpage, only broken links: https://i.sstatic.net/u7R0m.png I have tried looking into various solutions suggested by others:
Angularjs showing image blob
Display blob image in html with angularjs
AngularJS - Show byte array content as image
However, none of these solutions seem to work for me. I even attempted variations of the code snippet provided earlier:
<img data-ng-src="data:image/{{message.image_resolution}};base64,{{b64encoded(message.image_preview)}}"/>
Accompanied by the following JavaScript function:
$scope.b64encoded = function(image_preview){
//btoa(String.fromCharCode.apply(null, response.data[0].ClassImage.data));
$compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|file|ftp|blob):|data:image_preview\//);
return btoa(String.fromCharCode.apply(null, image_preview));
}
UPDATE After extensive troubleshooting, it turns out the issue was not with AngularJS or blobs but actually a Java problem:
byte[] previewSizeByte = baos.toByteArray();
. As a result, I ended up converting this to a text field, and modified my Java code to use BufferedImage for previews:
String base64String = imgToBase64String(preview, fileFormat);
This change led me to update the Java method as follows:
private String imgToBase64String(BufferedImage preview, String fileFormat) {
final ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
ImageIO.write(preview, fileFormat, Base64.getEncoder().wrap(os));
return os.toString(StandardCharsets.ISO_8859_1.name());
} catch (final IOException ioe) {
throw new UncheckedIOException(ioe);
}
}
I would like to express my gratitude to the members of Stack Overflow for their valuable insights and assistance throughout this troubleshooting process.