将img src属性设置为数据uri会导致错误消息

时间:2013-08-28 19:01:08

标签: google-apps-script

我希望将a的src属性设置为数据uri(data:image / png; base64等等)并且我遇到了错误:

  

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

我在Caja Playground上测试了一个小代码片段,但它也无法加载数据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 - (不应要求帐户登录...)

我开始认为这不是受支持的操作。

1 个答案:

答案 0 :(得分:1)

正确。 Caja现在不支持数据URI,因此不在Apps脚本中。请跟踪此Caja问题以获取更多详细信息 -

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