HOWTO: Upload Image to Google Drive using base64 and Google Apps Script Web App

8,842 views
Skip to first unread message

TimAI2

unread,
Mar 5, 2018, 6:49:08 AM3/5/18
to mitappinv...@googlegroups.com
### Also see HERE ###


This question was asked on the forum recently, and due to changes in google authentication has become more difficult to achieve. However, by using a "man in the middle" web app, it is possible to upload images to a private or public google drive folder, using base64 to convert the image to a string, and back again!

Credits must go to TANAIKE whose blog post on achieving this in several different ways, primarily using curl from a command line, lead me to a solution, and also to ghostfox for the base64 conversion extension over on thunkable.

I used the imagePicker to select files for upload, as this provides a full path to the file. It doesn't have to be done this way. To upload image assets in your app for the base64 extension, one must first copy the image to the internal sdcard.

To wet your whistle, here is a video of a sample app in action:


You are going to need three elements for all this to work (assuming you already have a google account in place!):

  1. A google drive folder to upload your files to, along with the ID for that folder
  2. A google apps script web app to handle the decoding of the base64 string
  3. An AI2 app to select and upload the file from
Lets get started

1. Google Drive Folder

Create a google drive folder for your uploads.

Note the folder ID

Decide on sharing options for your folder

2. Google Apps Script Web App

Somewhere else in your google drive, create a new Google Apps Script and name it accordingly

In the already created code.gs add the following code, overwriting the default code

function doGet(e) {
return message("Error: no parameters in doGet");
   
}

function doPost(e) {
 if (!e.parameters.filename || !e.parameters.file || !e.parameters.imageformat) {
   return message("Error: Bad parameters in doPost");
 } else {
   var imgf = e.parameters.imageformat[0].toUpperCase();
   var mime =
       (imgf == 'BMP')  ? MimeType.BMP
     : (imgf == 'GIF')  ? MimeType.GIF
     : (imgf == 'JPEG') ? MimeType.JPEG
     : (imgf == 'JPG')  ? MimeType.JPEG
     : (imgf == 'PNG')  ? MimeType.PNG
     : (imgf == 'SVG')  ? MimeType.SVG
     : false;
   if (mime) {
     var data = Utilities.base64Decode(e.parameters.file, Utilities.Charset.UTF_8);
     var blob = Utilities.newBlob(data, mime, e.parameters.filename);
     DriveApp.getFolderById('FOLDER ID HERE').createFile(blob);
     return message("Success");
   } else {
     return message("Error: Bad image format");
   }
 }
}

function message(msg) {
 return ContentService.createTextOutput(JSON.stringify({Result: msg })).setMimeType(ContentService.MimeType.JSON);
}

Replace FOLDER ID HERE with your chosen folder ID

Save the project and the publish as a web app, running the app as you, and available to anyone. 

You should be asked to authorise permissions, do so. If you are not asked then select the doGet() function and run this to get permissions sorted

You will also get a popup telling you the url to your published app (the one in the box). Note this down as you will need this for your AI2 app.

The web app requires three parameters: a filename, a file type, and the file itself ( in this case the base64 string )

3. AI2 App

These are the blocks used in the sample app from the video.

The only item you need to edit for it to work is the value for Web1.Url in the Button.click event. This is the url to the web app created earlier.

NOTE: after writing this howto, I discovered that some devices, or devices in some locales will convert jpeg extensions to jpg, and others from jpg extensions to jpeg

If when your image is selected by the listpicker and comes through as jpeg please change your ImagePicker1.AfterPicking blocks to the following: (and additional if statement to account for the jpeg extension





You should now have everything you need to upload files to google drive :)


uploadImage2GoogleDrive.aia

Terrell Glenn

unread,
Jul 30, 2019, 1:58:00 PM7/30/19
to MIT App Inventor Forum
Hi! I’m trying to do this exact same thing, but with a .txt file instead of an image, but I’m having a hard time. Is there a way to do this? Do I need to use this method, or is there a separate (probably simpler) method I should use? Thanks a ton!

TimAI2

unread,
Jul 30, 2019, 2:34:41 PM7/30/19
to MIT App Inventor Forum
You have a choice:

1. Either upload the contents of the text file to a cell in google drive

( you will need to set the contents of the text file to a variable to achieve this)

or

2. Upload the file to google drive (but you would need an intermediate php server)

TimAI2

unread,
Aug 1, 2019, 1:07:58 PM8/1/19
to MIT App Inventor Forum

TimAI2

unread,
Aug 8, 2019, 11:24:53 AM8/8/19
to MIT App Inventor Forum
A different method (for text only files) is to create a new file on Google Drive and fill it with the content of your text file on the app



// Create a text file with the content "Hello, world!"
DriveApp.createFile('New Text File', 'Hello, world!');


Reply all
Reply to author
Forward
0 new messages