Upload file to Google Drive

Hello I'm trying to upload files (pdfs to be exact) to a folder. I'm trying to achieve this using METRIC RAT AI2 - UpLOAD Any File To Google Drive with AI2 but after settings everything up it gives me a runtime error saying:


i tested with all the aia provided and happens to have the same issue.
i'm using android 13 and another issue is that my phone is denying permission to read/write external storage. I haven't tested using the apk. only companion

Any help would be great

Show your relevant blocks
Ensure you are able to read and convert the file to base64



image

also the PdfFileCopier1 is a custom extension i made with the help of A.I to get the absolute file path and i tested it and works.

as soon as i click choosing the file using activity starter , the runtime error occurs

Try the file picker component
http://ai2.appinventor.mit.edu/reference/components/media.html#FilePicker

Taifun

Or just use the SAF extension to pick and copy the pdf to ASD

i was using Filepicker but i changed it to activitystarter so i can choose only pdf files

sure but the extension that i have does copy the pdf to asd
and i tested it. So i don't need that. I want an answer to why it shows that runtime error.
before using the solution provided by you , i managed to manually program to encode to base64 using kio4 base64 extension and upload it to google drive using apps script. But when i open the file it does'nt work and says its damaged. So it was a no go. Then i tested the base64 encoding using an online website by converting the file to base64 then manually putting that base64 string in app inventor. But the same error occured.

There are too many unknowns here, including the original pdf.

I may get time later to put together a working example.

You may want to try this:

AIA
copyThenUpload.aia (39.7 KB)

BLOCKS

SCRIPT

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 + '"}');
}

SCREEN

image

DRIVE

image

Test PDF (set in Documents directory)
LoremIpsum.pdf (57.2 KB)

Credits @vknow360 for SAF extension
Credits @TIMAI2 for Base64 extension

ref1
ref2

it Worked!! Thank You So much for your Help!! I Appreciate your time for helping me
@TIMAI2
@Taifun

Did it also work for your pdf that previously failed?

Yea!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.