EasyE
EasyE

Reputation: 580

Display image is not showing up in safari, however in works fine in IE11

Background

I currently have a test webpage which I will need to work on both Windows and Mac OS environments. Currently I have a block of code which works on IE however when tested on a Mac it does not work.

Problem

This code is meant to dynamically create the following div's dynamically and then populate the img tag with the base64 results that I am getting from a call out to a service. The issue is it never displays in safari, however it displays on IE.

Code

$("#listView").kendoPanelBar({
                expandMode: "single",
                select: function (e) {
                var retrievedContent = dataSource._data;
                   for (var x = 0; x < retrievedContent.length; x++) {
                          if (e.item.dataset.uid === retrievedContent[x].uid) {
                           selectedContent = retrievedContent[x];
                 $.when(GetImgB64(selectedContent.ServerRelativeUrl)).done(function (imageB64) {
                       if (imageB64) {
                               var formattedB64 = "data:image/jpeg;base64," + imageB64;
                               $(".destroyWaiting").remove();
                               $(e.item, ".topTabPanel").append('<div class="destroy">&nbsp;&nbsp;Content : <button type="button" class="insertButton k-primary" id="button1" style="border: 2px none; border-radius: 25px; margin-left: 15px; margin-top: 5px;">Insert</button></div>');
                               $(e.item).append('<div class="destroy" style="margin-top: 5px; border: 1px solid darkgray;"><p></p><img class="img-responsive" src="' + formattedB64 + '" /></div>');
                               $(".insertButton").each(function (e) {
                                   $(this).click(function (d) {
                                      insertImages(imageB64);
                                    });
                                 });
                            }                        
                            else {
                                    FeedBackMessage("No Result For Criteria");
                                    }
                                });
                            }
                        }
                    else {
                        $(e.item).find(".destroy").remove();
                    }
                },
                collapse: function (e) {
                    $(e.item).find(".destroy").remove();
                }
            });

Upvotes: 1

Views: 1851

Answers (2)

Tad Macy
Tad Macy

Reputation: 11

My solution was different. Images returned as base64 string from a REST service worked everywhere except on Safari desktop or mobile.

After a day of research and experimentation, I found that the IMG SRC attribute must apply the prefix "data:image/jpeg;base64," as so:

document.getElementById("targetImg").src = "data:image/jpeg;base64," + defaultAvatarBase64String;

That is, something like this failed:

var imageSrc = "data:image/jpeg;base64," + defaultAvatarBase64String;
document.getElementById("targetImg").src = imageSrc

Turns out the REST service applied the "data:image/jpeg;base64," prefix. REST service had to be revised to return only the base64 image string.

Hope this helps...

Upvotes: 1

Raphael Rafatpanah
Raphael Rafatpanah

Reputation: 19967

Safari requires a base64 string that has a character count divisible by 4.

Use this:

if (imageB64) {

  while (imageB64.length % 4 > 0) {
    imageB64 += '=';
  }

  var formattedB64 = "data:image/jpeg;base64," + imageB64;
  $(".destroyWaiting").remove();
  $(e.item, ".topTabPanel").append('<div class="destroy">&nbsp;&nbsp;Content : <button type="button" class="insertButton k-primary" id="button1" style="border: 2px none; border-radius: 25px; margin-left: 15px; margin-top: 5px;">Insert</button></div>');
  $(e.item).append('<div class="destroy" style="margin-top: 5px; border: 1px solid darkgray;"><p></p><img class="img-responsive" src="' + formattedB64 + '" /></div>');
  $(".insertButton").each(function (e) {
    $(this).click(function (d) {
      insertImages(imageB64);
    });
  });
}

Source: Base64 image tag in safari did not showed up

Upvotes: 3

Related Questions