如何下载我从Google Picker API中选择的文件?

时间:2015-03-20 09:34:05

标签: javascript php google-picker

我正在PHP网站中实施Google Picker。我可以从Google Picker API获取文件ID,也可以使用JavaScript下载该文件。以下是我在setCallback(pickerCallback)函数中调用的回调函数。

function pickerCallback(data) {
    if (data.action == google.picker.Action.PICKED) {
      var fileId = data.docs[0].id;
      document.getElementById('googleFileId').value = fileId;
      var name = data.docs[0].name;
      var url = data.docs[0].url;
      var accessToken = gapi.auth.getToken().access_token;
      var request = new XMLHttpRequest();
      request.open('GET', 'https://www.googleapis.com/drive/v2/files/' + fileId);
      request.setRequestHeader('Authorization', 'Bearer ' + accessToken);
      request.addEventListener('load', function() {
          var item = JSON.parse(request.responseText);
          window.open(item.webContentLink,"_self"); //Download file in Client Side 
      });
      request.send();
    }
    var message = 'File ID of choosen file : ' + fileId;
    document.getElementById('result').innerHTML = message;
}

我可以将文件ID传递给PHP,但要下载文件,我必须再次进行身份验证。任何人都可以帮助如何在PHP中继续下载文件吗?

Google Developers页面中有一个管理下载帮助,但它不适用于我https://developers.google.com/drive/web/manage-downloads

发现了一个与此类似的问题,但没有解答如何在后端Download file right after picked file from google picker下载文件。

1 个答案:

答案 0 :(得分:0)

您必须为pick操作实现回调。看看我的实现:



    var buildPicker = function(parentId) {

      var pickerCallback = function(data) {
        if (data[google.picker.Response.ACTION] === google.picker.Action.PICKED && data.viewToken[0] !== 'upload') {
          var docs = data[google.picker.Response.DOCUMENTS];
          for (var d = 0; d < docs.length; d++) {
            downloadFile(docs[d].id);
          }
        }
      };

      GAuth.getToken().then(function(token) {
        var picker = new $window.google.picker.PickerBuilder()
          .addView(new google.picker.DocsUploadView().setParent(parentId))
          .addView(new google.picker.DocsView().setParent(parentId).setIncludeFolders(true))
          .setDeveloperKey(apiKey)
          .setOAuthToken(token.access_token)
          .setCallback(pickerCallback);

        picker.enableFeature(google.picker.Feature.MULTISELECT_ENABLED);
        picker.build().setVisible(true);
      });



    };

    var downloadFile = function(fileId) {

      getFile(fileId).then(function(file) {
        var downloadUrl;
        if (angular.isDefined(file.exportLinks)) {
          downloadUrl = file.exportLinks['application/pdf'];
        } else {
          downloadUrl = file.webContentLink;
        }
        var $idown;
        var makeiFrame = function(url) {
          if ($idown) {
            $idown.attr('src', url);
          } else {
            $idown = $('<iframe>', {
              id: 'idown',
              src: url
            }).hide().appendTo('body');
          }
        };
        makeiFrame(downloadUrl);
      });
    };


     // Implemented with https://github.com/maximepvrt/angular-google-gapi. But any other implementation will be fine as well
    var getFile = function(fileId) {
      var parameters = {
        'fileId': fileId
      };
      return GApi.executeAuth('drive', 'files.get', parameters);
    };
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
&#13;
&#13;
&#13;