Google Drive Codes

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

<!

DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Upload Videos to Google Drive</title>
</head>
<body>
<h1>Upload Videos to Google Drive</h1>
<form id="uploadForm">
<input type="file" id="fileInput" multiple>
<input type="text" id="fileName" placeholder="Enter new name (optional)">
<button type="submit">Upload</button>
</form>
<script src="https://apis.google.com/js/api.js"></script>
<script src="your_script.js"></script>
</body>
</html>

function handleClientLoad() {
gapi.load('client:auth2', initClient);
}

async function initClient() {


await gapi.client.init({
apiKey: 'YOUR_API_KEY',
clientId: 'YOUR_CLIENT_ID',
scope: 'https://www.googleapis.com/auth/drive.file',
discoveryDocs:
['https://www.googleapis.com/discovery/v1/apis/drive/v3/rest']
});
}

async function uploadFile(file, newName) {


const metadata = {
name: newName || file.name,
mimeType: file.type,
};

const form = new FormData();


form.append('metadata', new Blob([JSON.stringify(metadata)], { type:
'application/json' }));
form.append('file', file);

const accessToken = gapi.auth.getToken().access_token;


const response = await fetch('https://www.googleapis.com/upload/drive/v3/files?
uploadType=multipart', {
method: 'POST',
headers: new Headers({ 'Authorization': 'Bearer ' + accessToken }),
body: form,
});

const result = await response.json();


console.log(result);
}

document.getElementById('uploadForm').onsubmit = async function (event) {


event.preventDefault();
const files = document.getElementById('fileInput').files;
const newName = document.getElementById('fileName').value;

for (const file of files) {


await uploadFile(file, newName);
}
};

You might also like