inquisitive
inquisitive

Reputation: 3974

Google App Script, JavaScript FileReader is not a function

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

Upvotes: 1

Views: 1784

Answers (1)

baarkerlounger
baarkerlounger

Reputation: 1226

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.

Upvotes: 2

Related Questions