Skip to content
Advertisement

FileReader not defined in Apps Script

I am trying to use FileReader.readDataAsUrl() to send a blob object in a JSON file. But I keep getting the error Reference Error: “FileReader” is not defined. Is there a way that I need to implement it since I am using Apps Script? I see an issue someone had where they set their sandbox to IFRAME and it fixed it, but everywhere I see says that is the default mode now. The function where i actuallly initialize FileReader currently, is in buildPackage().

function doGet() {
  var response = getJSON();
  
  console.log("SENT DATA");
  return ContentService.createTextOutput(response).setMimeType(ContentService.MimeType.JSON);
}

function getJSON() {
  console.log("BUILDING JSON");

  var folder = DriveApp.getFolderById("1WT4KihCV5kXvmnO80Z9lPuvjoEH5vR63").getFiles();
  var files = {};
  
  while (folder.hasNext()) {
    var file = folder.next();

    var fileName = file.getName();
    var filePackage = buildPackage(file);

    files[fileName] = filePackage;
    console.log(filePackage);
  }

  return JSON.stringify(files);
}

function buildPackage(file) {
  //build the package to be sent holding the downloadurl, mime type, and blob
  var package = {};
  var fileReader = new FileReader();

  package["downloadUrl"] = file.getDownloadUrl();

  package["mime"] = file.getMimeType();

  var blob = file.getBlob();
  package["blob"] = blob;

  return package;
}

Here is my relevant code, I have my webapp pushed as accessible to anyone with the link, and runs as me. I removed any parts of my script using FileReader other than when i initialize it, and I still hit the error. I feel like I’m just missing something basic but i can’t find anything online thats helped so far.

Advertisement

Answer

FileReader object belongs to File API not to JavaScript programming language either to Google Apps Script. Considering this, it’s not possible to call it from the server side code, it can only be used from the client-side HTML/CSS/JavaScript code. Please see the first related question to learn how to convert a Blob into a base64 string, the second related question for code examples that use FileReader in a Google Apps Script web application.

Related

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement