簡體   English   中英

如何下載我從Google Picker API中選擇的文件?

[英]How to download a file that I have chosen from Google Picker API?

我正在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頁面上有一個Manage Downloads幫助,但不適用於我https://developers.google.com/drive/web/manage-downloads

發現了一個與此問題類似的問題,但是在從Google Picker拾取文件后立即在后端下載文件中如何下載文件沒有答案。

您必須為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); }; 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM