Google drive api 在Javascript客户端中从Google Drive下载文件

Google drive api 在Javascript客户端中从Google Drive下载文件,google-drive-api,google-docs-api,google-photos-api,Google Drive Api,Google Docs Api,Google Photos Api,我正在尝试将Google Drive集成到我的angular应用程序中,这样我们的用户就可以从文档中复制内容,并将他们的图像下载到我的应用程序中。根据,我使用下面的代码来获取文件 var request = gapi.client.drive.files.get({ 'fileId': fileId }); var temp = this; request.execute(function (resp) { }); 但在响应中,我只得到文件名和ID。

我正在尝试将Google Drive集成到我的angular应用程序中,这样我们的用户就可以从文档中复制内容,并将他们的图像下载到我的应用程序中。根据,我使用下面的代码来获取文件

var request = gapi.client.drive.files.get({
        'fileId': fileId
    });
     var temp = this;
     request.execute(function (resp) {
});
但在响应中,我只得到文件名和ID。下载文件功能不需要下载URL。 答复:

{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 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);
    }
}
我遗漏了什么吗?从客户端的驱动器下载文件是否正确?

问题1:
  • 您想从驱动器API下载一个文件
  • 您的访问令牌可用于下载文件
  • 您有下载该文件的权限
  • 您正在使用驱动器API中的files.get方法。在本例中,该文件不是Google文档
  • 您希望使用gapi和Javascript来实现这一点
如果我的理解是正确的,这次修改怎么样?请把这看作是几个可能的答案之一

修改点:
  • 要使用files.get-In-Drive API方法下载文件,请使用
    alt=media
    作为查询参数。当这反映到gapi时,请将
    alt:“media”
    添加到请求对象
修改脚本: 当您的脚本被修改时,它将变成如下所示

发件人: 致: 参考:
问题2:
  • 您想以DOCX格式下载Google文档
在这种情况下,请使用files.export方法,如下所示

示例脚本:
  • 在本例中,
    fileId
    是Google文档的文件ID。请小心这个
参考:

这对于下载图像非常有用

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;
    }

我不得不为我糟糕的英语水平道歉。不幸的是,我无法理解你的目标。那么我能问一下你的问题吗?是否要检索文件ID的“下载URL”?或者您想下载一个文件ID为的文件?你想实现哪一个?@Tanaike我想下载这个文件,谢谢你的回复。根据你的回复,我提出了一个修改后的脚本。你能确认一下吗?如果我误解了你的问题,而这不是你想要的方向,我道歉。谢谢你的回答。我可以使用alt:“media”来获取图像文件。如何获取Google文档内容?我收到错误->原因:“fileNotDownloadable”消息:“只能下载包含二进制内容的文件。使用导出Google文档文件。”.docxfiles@VBC谢谢你的回复。我很高兴你的问题解决了。关于你的第二个问题,我在回答中添加了一个示例脚本。你能确认一下吗?如果这不是你想要的方向,我道歉。工作正常。谢谢你的帮助time@VBC感谢您的回复和测试。我很高兴你的问题解决了。
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.

});
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.

});
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;
    }