UPDATED! HOWTO - Upload Any File to Google Drive !

2057 views
Skip to first unread message

TimAI2

unread,
Aug 25, 2019, 9:35:08 AM8/25/19
to mitappinv...@googlegroups.com
With many thanks to Juan Antonio, who developed THIS extension, we are now able to upload any file (as far as I have tested)
to Google Drive from within AI2, using base64 encoding and a simple google web app.

I have prepared a simple example to demonstrate how this is done, and can be used as a base to replace the HOWTOs I have done 
for uploading files and OCR for images and PDFs using Google Drive and Docs.

I recommend you keep original upload file sizes below 5mb

Tested on the following file types so far:

3gp,csv,gif,htm,html,jpeg,jpg,mp3,mp4,pdf,png,svg,txt,wav,webm,zip


The workflow:

  1. Get the file and its path and filename from either assets or the virtual sdcard
  2. Get the mimetype of the file using blocks from the extension
  3. Convert the file to a base64 encoded string
  4. Send these three things: filename, mimetype and encoded string to the web app
  5. The web app decodes the encoded string, and saves the file with the supplied filename,
    and with the correct mimetype to your folder specified in the web app
  6. The web app then returns a "success" message, if successful.

WEB APP SCRIPT

function doPost(e) {
  var data = Utilities.base64Decode(e.parameters.data);
  var blob = Utilities.newBlob(data, e.parameters.mimetype, e.parameters.filename);
  DriveApp.getFolderById('<YOUR FOLDER ID HERE>').createFile(blob);
  return ContentService.createTextOutput("Your File Successfully Uploaded");
   }

Remember to publish and create a new version each time you change something in the script. The script should run as the owner of the google account, and be accessible to "anyone,even anonymous"

Get the URL to the web app script when you publish, to use in your blocks

BLOCKS

simpleexampleUAF2GD.png


Remember to replace the correct URL for the web app script to the variable at the top of the blocks


AIA and Video of app in action attached after editing


Many thanks again to Juan, none of this would be possible without your fine efforts :)



SimpleExample_UploadAnyFileToGD_blank.aia
uploadGD.webm

ABG

unread,
Aug 25, 2019, 5:22:37 PM8/25/19
to MIT App Inventor Forum
(added to Files section of FAQ)
ABG

Ezra Epriputra

unread,
Aug 26, 2019, 11:36:07 AM8/26/19
to MIT App Inventor Forum
Thanks..this is what im looking for...
Best...
Reply all
Reply to author
Forward
0 new messages