2016-12-16 50 views
-2

我想要在Core Php上传文件并从本地检索它,并从Google Drive中选择。其中我需要一个代码从谷歌驱动器中选择一个特定的文件,并在本地上传它。我没有一个代码的想法,所以做到这一点。在Core Php上传文件,并在本地检索它

回答

0

首先创建filepicker.php文件,并写入下面提到代码:

<!DOCTYPE html> 
    <html> 
    <head> 
     <meta charset="UTF-8" /> 
     <title>Google Drive File Picker Example</title> 
    <style> 
    .button { 
     display: inline-block; 
     border-radius: 5px; 
     background-color: white; 
     border: 1; 
     color: black; 
     text-align: center; 
     font-size: 18px; 
     padding: 0px; 
     width: 150px; 
     height: 35px; 
     transition: all 0.5s; 
     cursor: pointer; 
     margin: 10px; 
    } 
    </style> 
    </head> 
    <body> 
     <button type="button" class="button" style="vertical-align:middle" id="pick" style="bgcolor:blue;"><span align="center"><img src="Google_Drive.png" height="20px"> Google Drive</span></button> 
     <script src="filepicker.js"></script> 
     <?php 
     echo "<script> function hi() { alert('hi') } </script>"; 
     echo "<button type='button' onclick='hi()'> click </button>"; 
     ?> 
     <script> 
      function initPicker() { 
       var picker = new FilePicker({ 
        apiKey: 'AIzaSyB2c-5sNr6U4VpbIgvcDUHsnSwqT8ia0sM', 
        clientId: '649363830487-g2bckoufvgpeme8r5a9jngdp6c509mnq', 
        buttonEl: document.getElementById('pick'), 
        onSelect: function(file) { 
        console.log(file); 
        var fid = file.id; 
      //  document.write('Selected ' + file.title + '<br>'); 
        var temp = 'drive.google.com/file/d/' + fid; 
        var url = document.write('<b>Selected: </b> ' + 'drive.google.com/file/d/' + fid + '<br>'); 
        alert(temp); 

        document.write("<a href='//"+temp+"'/> Click</a>"); 
        } 
       }); 
      } 
     </script> 

     <script src="https://www.google.com/jsapi?key=AIzaSyB2c-5sNr6U4VpbIgvcDUHsnSwqT8ia0sM"></script> 
     <script src="https://apis.google.com/js/client.js?onload=initPicker"></script> 

     <?php 
     ?> 
     </body> 
    </html> 

然后创建一个名为js文件作为filepicker.js

/**! 
* Google Drive File Picker Example 
* By Daniel Lo Nigro (http://dan.cx/) 
*/ 
(function() { 
    /** 
    * Initialise a Google Driver file picker 
    */ 

    var FilePicker = window.FilePicker = function(options) { 
     // Config 
     this.apiKey = options.apiKey; 
     this.clientId = options.clientId; 

     // Elements 
     this.buttonEl = options.buttonEl; 

     // Events 
     this.onSelect = options.onSelect; 
     this.buttonEl.addEventListener('click', this.open.bind(this));  

     // Disable the button until the API loads, as it won't work properly until then. 
     this.buttonEl.disabled = true; 

     // Load the drive API 
     gapi.client.setApiKey(this.apiKey); 
     gapi.client.load('drive', 'v2', this._driveApiLoaded.bind(this)); 
     google.load('picker', '1', { callback: this._pickerApiLoaded.bind(this) }); 
} 

    FilePicker.prototype = { 
     /** 
     * Open the file picker. 
     */ 
     open: function() {  
      // Check if the user has already authenticated 
      var token = gapi.auth.getToken(); 
      if (token) { 
       this._showPicker(); 
      } else { 
       // The user has not yet authenticated with Google 
       // We need to do the authentication before displaying the Drive picker. 
       this._doAuth(false, function() { this._showPicker(); }.bind(this)); 
      } 
     }, 

     /** 
     * Show the file picker once authentication has been done. 
     * @private 
     */ 
     _showPicker: function() { 
      var accessToken = gapi.auth.getToken().access_token; 
      this.picker = new google.picker.PickerBuilder(). 
       addView(google.picker.ViewId.DOCUMENTS). 
       setAppId(this.clientId). 
       setOAuthToken(accessToken). 
       setCallback(this._pickerCallback.bind(this)). 
       build(). 
       setVisible(true); 
     }, 

     /** 
     * Called when a file has been selected in the Google Drive file picker. 
     * @private 
     */ 
     _pickerCallback: function(data) { 
      if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) { 
       var file = data[google.picker.Response.DOCUMENTS][0], 
        id = file[google.picker.Document.ID], 
        request = gapi.client.drive.files.get({ 
         fileId: id 
        }); 

       request.execute(this._fileGetCallback.bind(this)); 
      } 
     }, 
     /** 
     * Called when file details have been retrieved from Google Drive. 
     * @private 
     */ 
     _fileGetCallback: function(file) { 
      if (this.onSelect) { 
       this.onSelect(file); 
      } 
     }, 

     /** 
     * Called when the Google Drive file picker API has finished loading. 
     * @private 
     */ 
     _pickerApiLoaded: function() { 
      this.buttonEl.disabled = false; 
     }, 

     /** 
     * Called when the Google Drive API has finished loading. 
     * @private 
     */ 
     _driveApiLoaded: function() { 
      this._doAuth(true); 
     }, 

     /** 
     * Authenticate with Google Drive via the Google JavaScript API. 
     * @private 
     */ 
     _doAuth: function(immediate, callback) {  
      gapi.auth.authorize({ 
       client_id: this.clientId + '.apps.googleusercontent.com', 
       scope: 'https://www.googleapis.com/auth/drive', 
       immediate: immediate 
      }, callback); 
     } 
    }; 

}()); 
+0

这个答案肯定会解决你的上传文件的问题和检索通过谷歌驱动器。 –