Wednesday, June 7, 2017

JavaScript code to show image from raw image content bytes (blob)

<div> <img id="attachmentImage" /> </div> // attachmentCntnt : this variable contains raw bytes array of the attachment image // attachmentType : keeps the image type such as jpg/jpeg/png/... // _.defer and _.isNull are methods from underscore.js library var imageUrl = null; var attachmentCntntU8Ary = new Uint8Array(attachmentCntnt); var blob = new Blob([attachmentCntntU8Ary], { type: "image/" + attachmentType }); var urlCreator = window.URL || window.webkitURL; imageUrl = urlCreator.createObjectURL(blob); _.defer(function (t) { if (!_.isNull(imageUrl)) { var img = document.querySelector("#attachmentImage"); img.src = imageUrl; }; }, self);

Share/Bookmark

No comments: