How to upload multiple images to Google Drive using AppsScript
Here's an example of how to create a web app using Google Apps Script that allows users to upload multiple images to Google Drive:
Create a new Google Apps Script project in your Google Drive by going to script.google.com.
Replace the existing code in the "Code.gs" file with the following code:
Code.gs
function doGet() {
return HtmlService.createHtmlOutputFromFile('Page')
.setTitle('Image Uploader');
}
function processForm(form) {
var files = form.uploadedFiles;
var folder = DriveApp.createFolder('Uploaded Images');
for (var i = 0; i < files.length; i++) {
var file = files[i];
var blob = file.blob;
var name = file.name;
var contentType = file.contentType;
var newFile = folder.createFile(blob);
newFile.setName(name);
newFile.setContentType(contentType);
}
return 'Files uploaded successfully!';
}
Create a new HTML file in the project by clicking on "File > New > Html file" and naming it "Page". Replace the existing code in the "Page.html" file with the following code:
Page.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<h1>Image Uploader</h1>
<form id="myForm" onsubmit="submitForm(this)">
<input type="file" name="uploadedFiles" multiple>
<br><br>
<input type="submit" value="Upload">
</form>
<div id="output"></div>
<script>
function submitForm(form) {
var outputDiv = document.getElementById('output');
google.script.run.withSuccessHandler(function(response) {
outputDiv.innerHTML = response;
}).processForm(form);
}
</script>
</body>
</html>
Save your changes and publish the web app by clicking on "Publish > Deploy as web app". Make sure to choose the appropriate settings, such as the version, access level, and project credentials. Once you've published the web app, you'll be given a URL that you can share with others.
That's it! When users visit the URL for your web app, they'll be able to select multiple image files to upload to a new folder in their Google Drive. The uploaded files will be renamed and assigned the appropriate content type. The web app will display a message indicating whether the upload was successful or not.
-----Have a nice day!-----