使用javascript api从我们的网站页面上传Google云端硬盘帐户中的文件

时间:2015-10-09 07:17:06

标签: javascript google-api google-drive-api

我正在尝试使用网页表单将文件上传到我的Google云端硬盘帐户。我选择了一个文件,我必须使用Google drive将其上传到我的JavaScript API,所以,如果有任何解决方案让任何人帮助我。

以下是访问Google云端硬盘的一个示例代码,但我无法获取上传文件代码。

<!--Add a button for the user to click to initiate auth sequence -->
<button id="authorize-button" style="visibility: hidden">Authorize</button>
<script type="text/javascript">
  var clientId = '######';
  var apiKey = 'aaaaaaaaaaaaaaaaaaa';
  // To enter one or more authentication scopes, refer to the documentation for the API.
  var scopes = "https://www.googleapis.com/auth/drive";

  // Use a button to handle authentication the first time.
  function handleClientLoad() {
    gapi.client.setApiKey(apiKey);
    window.setTimeout(checkAuth,1);
  }

  function checkAuth() {
    gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: true}, handleAuthResult);
  }

  function handleAuthResult(authResult) {
    var authorizeButton = document.getElementById('authorize-button');
    if (authResult && !authResult.error) {
      authorizeButton.style.visibility = 'hidden';
      makeApiCall();
    } else {
      authorizeButton.style.visibility = '';
      authorizeButton.onclick = handleAuthClick;
    }
  }

  function handleAuthClick(event) {
    gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthResult);
    return false;
  }

  // Load the API and make an API call.  Display the results on the screen.
  function makeApiCall() {
    gapi.client.load('drive', 'v2', function() {

      var request = gapi.client.drive.files.list ( {'maxResults': 5 } );

      request.execute(function(resp) {          
        for (i=0; i<resp.items.length; i++) {
                var titulo = resp.items[i].title;
                var fechaUpd = resp.items[i].modifiedDate;
                var userUpd = resp.items[i].lastModifyingUserName;

                var fileInfo = document.createElement('li');
                fileInfo.appendChild(document.createTextNode('TITLE: ' + titulo + ' - LAST MODIF: ' + fechaUpd + ' - BY: ' + userUpd ));                
                document.getElementById('content').appendChild(fileInfo);
        }
      });        
    });
  }
</script>
<script src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>    
<p><b>These are 5 files from your GDrive :)</b></p>
<div id="content"></div>

0 个答案:

没有答案
相关问题