1

I'm trying to do an image preview functionality through google app script.

This is my code:

code.gs

function doGet(e) {
  return HtmlService.createTemplateFromFile('form')
    .evaluate() // evaluate MUST come before setting the NATIVE mode
    .setTitle('Details')
    .setSandboxMode(HtmlService.SandboxMode.NATIVE);
}

form.html

<!DOCTYPE html>
<html>
  <head>
    <script>
var loadFile = function(event) {
    var reader = new FileReader();
    reader.onload = function(){
      var output = document.getElementById('preview');
      output.src = reader.result;
    };
    reader.readAsDataURL(event.target.files[0]);
  };
    </script>
  </head>
  <body>
    <input type='file' id="imgInp" onchange="loadFile(event)" />
    <img id="preview" src="#" alt="your image" />
  </body>
</html>

When I deploy this code, it gives me the following error:

In Safari: undefined is not a constructor (evaluating 'new FileReader()')

In Chrome: Uncaught TypeError: FileReader is not a function

I doubted that FileReader may not be understood by the Google App Script interpreter(?), but I found this SO question and it seems to exist and be used.

I tried using IFRAME mode instead of NATIVE and it seem to work. It's weird though. I want to make it work through NATIVE

Community
  • 1
  • 1
inquisitive
  • 3,738
  • 6
  • 30
  • 56
  • I'm trying your code in Chrome and it seems like it works fine for me. Maybe your issue is with something else? – baarkerlounger Nov 19 '15 at 06:52
  • @db579: did you try the Native mode only? The same code that I posted? I tried in different system also. Can you tell your machine config, chrome version if at all the difference lies there. The code worked for me if I changed the mode from native to iframe in code.gs, which makes me think is it because of the mode. – inquisitive Nov 19 '15 at 07:08
  • Ok I'm able to reproduce your error now - I seem to get it in IFRAME mode also though – baarkerlounger Nov 19 '15 at 07:34

1 Answers1

2

NATIVE mode has been depreciated and as off next year any scripts calling it explicitly will be defaulted to IFRAME mode anyway. As such I'd suggest you work with IFRAME since it's working for you.

https://developers.google.com/apps-script/sunset

Since the code seems to work fine in regular javascript with "use strict" tag I guess the issue is with the NATIVE mode implementation itself but if that's the case it's not likely to be fixed now.

baarkerlounger
  • 1,217
  • 8
  • 40
  • 57