Skip to content

Instantly share code, notes, and snippets.

@pizzapanther
Last active February 25, 2022 15:57
Show Gist options
  • Save pizzapanther/8241364 to your computer and use it in GitHub Desktop.
Save pizzapanther/8241364 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);
@aryeharmon
Copy link

Uncaught ReferenceError: gapi is not defined

@jeggu96
Copy link

jeggu96 commented Jan 24, 2017

What is the "file" in function downloadGDriveFile (file) ???

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment