-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
42 lines (38 loc) · 1.22 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// script.js
const imageInput = document.getElementById('image-input');
const uploadBtn = document.getElementById('upload-btn');
const outputDiv = document.getElementById('output');
uploadBtn.addEventListener('click', async (e) => {
e.preventDefault();
const files = imageInput.files;
for (let i = 0; i < files.length; i++) {
const file = files[i];
try {
// Create a new Google Drive API client
const driveClient = await getDriveClient();
// Upload the file to Google Drive
await uploadFileToDrive(file, driveClient);
outputDiv.textContent += `Uploaded ${file.name} to Google Drive`;
} catch (error) {
console.error(error);
}
}
});
async function getDriveClient() {
const auth = new google.auth.GoogleAuth();
return auth.getClient();
}
async function uploadFileToDrive(file, driveClient) {
try {
// Create a new file in Google Drive
await driveClient.files.create({
body: {
name: file.name,
mimeType: 'image/jpeg',
content: await file.arrayBuffer(),
},
});
} catch (error) {
console.error(error);
}
}