How to return an image from a Google Apps Script Web App?
Asked Answered
C

3

8

I am trying to make a Web App with Google Apps Script that actually returns an image file instead of just text. I tried it as simple as that:

function doGet() {
    var file = DriveApp.getFileById('[redacted]');
    return file;
}

When publishing it as a web app and executing it, it just says

The script completed but did not return anything.

I know the file exists and is accessible, since I can print its MIME type, which is a proper image/png. So I'm missing something obvious here, but being not particularly well-versed in Google Apps Script, let alone web development of any kind, I don't know what that is I'm missing and searching for introductory material on returning an image from a Google Apps Script has not been particularly fruitful either, especially when I'm not realyl sure where to start at all.

Is it even possible to return an image file from a web app via Google Apps Script?

Convulsant answered 18/4, 2016 at 16:50 Comment(0)
N
4

doGet() is a reserved function name. The function name is reserved for listening to a GET request made to the published URL of the Web App. The GET request that is made to the published URL of the Web App is the "event" and the doGet() function is triggered by the event.

The doGet() function can return two different types of output.

  • HTML Output Object - HtmlService
  • Text Output Object - ContentService

The most common use of doGet() is to return HTML content to the browser. But using HtmlService to return file content won't work.

doGet() can also be used together with "Content Service" to return text content in various forms.

The text content can be returned as plain text, or the MIME Type of the text content can be set before returning it. The MIME type can be:

  • ATOM
  • CSV
  • ICAL
  • JAVASCRIPT
  • JSON
  • RSS
  • TEXT
  • VCARD
  • XML

But there is no MIME Type setting to return file types.

If the file can be converted to text content, returned, and then formatted in the browser, then that is a possibility. If you want to return something like an image file, then you would need to convert the image to a Blob, then get the blob as a string, and then return the string.

Neutral answered 18/4, 2016 at 18:3 Comment(0)
L
1

The function doGet must return an HtmlOutput object, not a file object. Such an object is created using HtmlService. Example:

function doGet() {
  var html = '<img src = "https://googledrive.com/host/{file id}">'; 
  return HtmlService.createHtmlOutput(html);
}

This example will no longer work after August 31, 2016, when Google shuts down Drive Hosting.

Hence, the suggestion is to host static content elsewhere and use Apps Script web apps to generate HTML serving them:

function doGet() {
  var html = '<img src = "https://example.com/filename">'; 
  return HtmlService.createHtmlOutput(html);
}
Lacking answered 18/4, 2016 at 18:1 Comment(0)
L
0

Suppose you have a server to call google script api. so you can encode and then decode your file contents to save in storage.

    function doGet() {
      var file = DriveApp.getFileById('[redacted]');
      const content = file.getBlob().getBytes();
      const encodedContent = Utilities.base64Encode(content);
      return encodedContent;
    }     
 
Laconia answered 21/4, 2023 at 18:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.