【问题标题】:[google-drive-api]How to handle google API's get() alt=media response[google-drive-api]如何处理 google API 的 get() alt=media response
【发布时间】:2016-06-10 10:20:32
【问题描述】:

我一直在尝试在我的 angularjs 应用中实现驱动 API。我想下载一个 pdf 文件,所以我使用以下代码。

$scope.getFile = function(id) {
  console.log(id);
  //console.log(fileId);
  if (id != null) {
    var request = gapi.client.drive.files.get({
      'fileId': id,
      'alt': 'media'
    });
    request.execute(function(resp) {
      console.log('response: ' + resp);
    });
  }
}

但我的 console.log 打印出“假”。我不知道为什么它真的让我抓狂我检查了检查元素中的网络选项卡,发现响应正确为 base64 字符串。

我还打电话给atob(resp),它说要解码的文本没有正确编码。但我使用了一个在线base64解码器,它可以完美地打开pdf。请帮帮我,我卡在这里了

【问题讨论】:

    标签: javascript angularjs api google-drive-api


    【解决方案1】:

    只需使用then 而不是execute

    var request = gapi.client.drive.files.get({
        'fileId': id,
        'alt': 'media'
    });
    request.then(function(resp) {
        console.log('response: ' + resp);
    });
    

    【讨论】:

    • 谢谢!你拯救了我的一天!
    猜你喜欢
    • 1970-01-01
    • 2022-09-30
    • 1970-01-01
    • 1970-01-01
    • 2018-07-29
    • 2013-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多