在Javascript客户端中从Google云端硬盘下载文件 [英] Downloading a file from Google Drive in Javascript client

查看:111
本文介绍了在Javascript客户端中从Google云端硬盘下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Google云端硬盘集成到我的角度应用程序中,以便我们的用户可以复制文档中的内容并将其图像下载到我的应用程序中.根据 file:get API文档,使用以下代码获取文件

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

但是在响应中我只得到文件名和ID.downloadFile函数不需要下载URL.响应:

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 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:

  • 您要从Drive API下载文件.
  • 您的访问令牌可用于下载文件.
  • 您具有下载文件的权限.
  • 您正在使用Drive API中的files.get方法.在这种情况下,该文件不是Google文档.
  • 您要使用带有Java的gapi实现此目的.
  • 如果我的理解是正确的,那么该修改如何?请认为这只是几个可能的答案之一.

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

    • 要使用Drive API中的files.get方法下载文件,请使用 alt = media 作为查询参数.当这反映到gapi时,请在请求对象中添加 alt:"media" .
    • 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.

    修改脚本后,它如下所示.

    When your script is modified, it becomes as follows.

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

    参考:

    • 下载文件
      • 您要以DOCX格式下载Google文档.

      在这种情况下,请按以下方式使用files.export方法.

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

      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.请注意这一点.
        • In this case, fileId is the file ID of Google Document. Please be careful this.
        • 这篇关于在Javascript客户端中从Google云端硬盘下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆