UpLOAD Any File To Google Drive with AI2
UPDATE: new improved method, August 2024
This improved method is courtesy of Tanaike on StackOverflow.
You still have to convert the file to base64, and provide a filename, but the blocks usage is much cleaner and more concise.
I am using my own Base64Convertor extension now.
If you are new to google apps script/web apps then you may want to review this guide
WEB APP SCRIPTS
This script will save the file to the root folder of your google drive.
Output is returned as a stringified JSON, which can then be converted to a dictionary in your app.
function doPost(e) {
var contents = JSON.parse(e.postData.contents.replace(/\n/g, ""));
var fileName = contents.filename;
var data = Utilities.base64Decode(contents.file);
var blob = Utilities.newBlob(data, e.postData.type, fileName);
var fileId = DriveApp.createFile(blob).getId();
return ContentService.createTextOutput('{"fileName": "'+ fileName + '", "fileId": "' + fileId + '"}');
}
This script will save the file to the folder that you specify by ID - replace "<FOLDER ID HERE>" with your folderId
function doPost(e) {
var contents = JSON.parse(e.postData.contents.replace(/\n/g, ""));
var fileName = contents.filename;
var data = Utilities.base64Decode(contents.file);
var blob = Utilities.newBlob(data, e.postData.type, fileName);
var fileId = DriveApp.getFolderById(<FOLDER ID HERE>).createFile(blob).getId();
return ContentService.createTextOutput('{"fileName": "'+ fileName + '", "fileId": "' + fileId + '"}');
}
BLOCKS
In this example I have files in the assets/media folder of the app, therefore calling that file path. In general you would use an absolute path to a file.
Previous method, still works...
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 (see UPDATE2 below regarding aia & aix files)
The workflow:
Get the file and its path and filename from either assets or the virtual sdcard
Get the mimetype of the file using blocks from the extension
Convert the file to a base64 encoded string
Send these three things: filename, mimetype and encoded string to the web app
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
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.createFile(blob);
return ContentService.createTextOutput("Your File Successfully Uploaded");
}
If you want the file ID of the uploaded file returned to the app, then use this script instead: (you will need to amend the workings of the Web1.GotText blocks accordingly)
function doPost(e) {
var data = Utilities.base64Decode(e.parameters.data);
var blob = Utilities.newBlob(data, e.parameters.mimetype, e.parameters.filename);
var fileID = DriveApp.createFile(blob).getId();
return ContentService.createTextOutput(fileID);
}
If you want to save the image to a specific folder, then use this script instead:
(you can either hard code the folder ID in the script, or add it to the parameters you send to the script from the app)
function doPost(e) {
var data = Utilities.base64Decode(e.parameters.data);
var blob = Utilities.newBlob(data, e.parameters.mimetype, e.parameters.filename);
var fileID = DriveApp.getFolderById(<FOLDER ID HERE>).createFile(blob).getId();
return ContentService.createTextOutput(fileID);
}
To then view or download the uploaded image in your app use the following url:
https://drive.google.com/uc?export=download&id=<fileID>
Remember to publish/deploy 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” (legacy editor), or "Anyone" (in new editor - NOT Anyone with a Google Account)
Get the URL to the web app script when you publish, to use in your blocks
BLOCKS
The file ai2logo.png is in the assets of the aia project. This is copied to the ASD for conversion to a base64 string.
Remember to replace the correct URL for the web app script to the variable at the top of the blocks
this example saves the file to the root of Google Drive (no folder is specified in the web app)
Many thanks again to Juan, none of this would be possible without your fine efforts 🙂