1

I'm hoping to set the src attribute of an to a data uri (data:image/png;base64,etc..) and I'm running into an error:

Rejecting .setAttribute( src , data:image/png;base64,image_data... )

I tested out a small code snippet on the Caja Playground, but it also was unable to load a data uri.

Code.gs

// Script-as-app template.
function doGet() {
  return HtmlService.createTemplateFromFile('index').evaluate();
}

function fetch(url) {
  var response = UrlFetchApp.fetch(url);

  return Utilities.base64Encode(response.getContent());
}

Index.html

<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>Image Test</title>
        <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.2.min.js"></script>
        <script>
        $(document).ready(function() {
            $("#test").click(function() {
                google.script.run.withSuccessHandler(function(data) {
                    window.console.log(data);

                    $("#photo1").attr("src", "data:image/png;base64," + data);
                }).fetch("http://upload.wikimedia.org/wikipedia/commons/thumb/3/30/Googlelogo.png/320px-Googlelogo.png");
            });
        });
        </script>
    </head>
    <body>
        <button id="test">Test</button>
        <img id="photo1" width="320" height="110" />
        <img id="photo2" width="320" height="110" src="http://upload.wikimedia.org/wikipedia/commons/thumb/3/30/Googlelogo.png/320px-Googlelogo.png" />
    </body>
</html>

Public link to Google Apps Script - (It shouldn't require account login...)

I'm beginning to think that this is not a supported operation.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
ShelbyZ
  • 1,494
  • 1
  • 14
  • 32
  • possible duplicate of [Using base64-encoded images with HtmlService in Apps Script](http://stackoverflow.com/questions/20656803/using-base64-encoded-images-with-htmlservice-in-apps-script) – hopper Jun 09 '15 at 17:54
  • I will agree that the questions are similar, but in a time sense of things this question existed prior to the linked question leading me to think of it as a the 'duplicate' in question. – ShelbyZ Jun 09 '15 at 19:39
  • You're quite right that your question was here first; I chose to flag this question as a duplicate (rather than the other) since I felt the answer on the other question was more detailed and useful. It wasn't intended as a slight on your question at all :) – hopper Jun 09 '15 at 19:49
  • I figured, I was just trying to better understand the process to be honest. I couldn't find any information pointing in one direction or another. Carry on. – ShelbyZ Jun 10 '15 at 11:33

1 Answers1

1

Correct. Data URIs are not supported today by Caja and hence not in Apps Script. Please track this Caja issue for more details -

https://code.google.com/p/google-caja/issues/detail?id=1558

Arun Nagarajan
  • 5,547
  • 1
  • 22
  • 19