Upload a file from the page to Drive
The page reads the chosen file as base64 and sends it; the server saves it in a folder and returns its link.
File Code.gs
const UPLOAD_FOLDER_ID = '';
function uploadFile(name, mimeType, base64) {
const blob = Utilities.newBlob(Utilities.base64Decode(base64), mimeType, name);
const file = DriveApp.getFolderById(UPLOAD_FOLDER_ID).createFile(blob);
return { id: file.getId(), url: file.getUrl() };
}
File js/app.html
<input type="file" id="pick">
<script>
document.getElementById('pick').addEventListener('change', e => {
const file = e.target.files[0];
if (!file) return;
const reader = new FileReader();
reader.onload = () => {
const base64 = String(reader.result).split(',')[1];
google.script.run
.withSuccessHandler(res => alert('Saved: ' + res.url))
.withFailureHandler(err => alert(err.message))
.uploadFile(file.name, file.type, base64);
};
reader.readAsDataURL(file);
});
</script>
Good to know
- google.script.run carries about 50 MB at most; keep uploads small (photos: resize in the browser first).
Let the AI put it in your appGAS AI Builder knows every recipe here. Describe your app and it uses this code where it fits, in a Google Apps Script project in your own Drive.
Use it in a projectMore in Web app
Who is using the app, and their roleReads the visitor's email and looks up their role in a Users tab; server functions check it before doing anything.doGet with pages split into filesindex.html pulls in css/app.html and js/app.html with include(); works on phones and inside an iframe.Call the server from the page with async/awaitA small helper turns google.script.run into a promise, with errors shown instead of lost.Return data the page can readTurns dates into text and drops values the page can't receive, so a result never arrives as null.