Downloading a file from Google Drive in Javascript client

雨燕双飞 提交于 2021-01-28 05:28:33

问题


I'm trying to integrate the Google Drive in my angular application So that our users can copy the content from docs and download their images to my application. As per the file:get API Documentation, I'm using the below code to get the file

var request = gapi.client.drive.files.get({
        'fileId': fileId
    });
     var temp = this;
     request.execute(function (resp) {
});

But in the response I'm getting only File name and ID.There is no download URL which is required for downloadFile function. Response:

{kind: "drive#file", 
  id: "1KxxxxxxxxxxxxxxycMcfp8YWH2I",
   name: " Report-November", 
   mimeType: "application/vnd.google-apps.spreadsheet", 
    result:{
kind: "drive#file"
id: "1K7DxawpFz_xiEpxxxxxxxblfp8YWH2I"
name: "  Report-November"
mimeType: "application/vnd.google-apps.spreadsheet"
  }
}



Download File Function:

/**
 * Download a file's content.
 *
 * @param {File} file Drive File instance.
 * @param {Function} callback Function to call when the request is complete.
 */
 downloadFile(file, callback) {
    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 () {
            callback(xhr.responseText);
        };
        xhr.onerror = function () {
            callback(null);
        };
        xhr.send();
    } else {
        callback(null);
    }
}

Am I missing anything? Is it the right approach to download a file from Drive in the client side?


回答1:


Question 1:

  • You want to download a file from Drive API.
  • Your access token can be used for downloading the file.
  • You have the permission for downloading the file.
  • You are using the method of files.get in Drive API. In this case, the file is not Google Docs.
  • You want to achieve this using gapi with Javascript.

If my understanding is correct, how about this modification? Please think of this as just one of several possible answers.

Modification point:

  • In order to download a file using the method of files.get in Drive API, please use alt=media for the query parameter. When this is reflected to gapi, please add alt: "media" to the request object.

Modified script:

When your script is modified, it becomes as follows.

From:
var request = gapi.client.drive.files.get({
        'fileId': fileId
    });
     var temp = this;
     request.execute(function (resp) {
});
To:
gapi.client.drive.files.get({
  fileId: fileId,
  alt: "media"
}).then(function(res) {

  // In this case, res.body is the binary data of the downloaded file.

});

Reference:

  • Download files

Question 2:

  • You want to download Google Document as a DOCX format.

In this case, please use the files.export method as follows.

Sample script:

gapi.client.drive.files.export({
  fileId: fileId,
  mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
}).then(function(res) {

  // In this case, res.body is the binary data of the downloaded file.

});
  • In this case, fileId is the file ID of Google Document. Please be careful this.

Reference:

  • Download a Google Document



回答2:


This is good for downloading an image

async function downloadFile(fileId: string, mimeType: string) {
        const res = await gapi.client.drive.files.get({
            fileId,
            alt: 'media'
        });
        const base64 = 'data:' + mimeType + ';base64,' + Buffer.from(res.body, 'binary').toString('base64');
        return base64;
    }


来源:https://stackoverflow.com/questions/60839431/downloading-a-file-from-google-drive-in-javascript-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!