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

标签 google-drive-api google-docs-api google-photos-api

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

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:

  • 您想从 Drive API 下载文件。
  • 您的访问 token 可用于下载文件。
  • 您有下载文件的权限。
  • 您正在使用 Drive API 中的 files.get 方法。在这种情况下,该文件不是 Google 文档。
  • 您想使用带有 Javascript 的 gapi 来实现这一点。

  • 如果我的理解是正确的,这个修改怎么样?请将此视为几种可能的答案之一。

    修改点:
  • 要使用Drive API中的files.get方法下载文件,请使用alt=media对于查询参数。当这反射(reflect)到gapi时,请添加alt: "media"到请求对象。

  • 修改后的脚本:

    当你的脚本被修改时,它变成如下。

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

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

    引用:
  • Download files

  • 问题2:
  • 您想以 DOCX 格式下载 Google 文档。

  • 在这种情况下,请使用如下所示的 files.export 方法。

    示例脚本:
    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.
    
    });
    
  • 在这种情况下,fileId是 Google 文档的文件 ID。请注意这一点。

  • 引用:
  • Download a Google Document
  • 关于google-drive-api - 在 Javascript 客户端中从 Google Drive 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60839431/

    相关文章:

    java - 谷歌驱动器 SDK 上传演示文稿时出错

    google-sheets - 如何获取注释或评论

    php - 阅读谷歌文档电子表格

    java - 在不提示用户登录的情况下在服务器端验证 Google Drive API

    google-app-engine - Google 云端硬盘按服务帐户更改权限

    android - 如果我的 Android 设备没有 Google Play 服务,如何添加 Google Drive 帐户

    google-photos - 是否可以删除通过 Google Photos API 创建的照片或相册?

    python - Google 相册 API - 新版本?

    objective-c - 如何获取所有 mime 类型 Google Drive Api IOS