Skip to content

Instantly share code, notes, and snippets.

@stunzz
Forked from pizzapanther/gdrive_download.js
Created February 25, 2022 15:57
Show Gist options
  • Save stunzz/bf95878316988a819fedfe15d50f5ea5 to your computer and use it in GitHub Desktop.
Save stunzz/bf95878316988a819fedfe15d50f5ea5 to your computer and use it in GitHub Desktop.
Download A Google Drive File via Javascript
function downloadGDriveFile (file) {
if (file.downloadUrl) {
var accessToken = gapi.auth.getToken().access_token;
var xhr = new XMLHttpRequest();
xhr.open('GET', file.downloadUrl);
xhr.setRequestHeader('Authorization', 'Bearer ' + accessToken);
xhr.onload = function() {
var content = xhr.responseText;
do_something_with_file_content(file.title, content);
};
xhr.onerror = function() {
alert("Download failure.");
};
xhr.send();
}
else {
alert("Unable to download file.");
}
}
var request = gapi.client.drive.files.get({'fileId': fid});
request.execute(downloadGDriveFile);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment