用户通过javascript上传文件到谷歌驱动器?

Jac*_*rie 5 javascript google-drive-api

我有一个网络应用程序,我希望用户能够从他们的计算机添加文件并将其上传到我的谷歌驱动器.我有选择文件按钮工作,但我不知道该功能应该如何访问我的谷歌驱动器帐户,并通过单击按钮发送文件.

<h4>Upload a file:</h4>
    <div class="form-group">
        <input type="file" id="fileInput" name="fileInput"/>
    </div><br>
Run Code Online (Sandbox Code Playgroud)

我将它放在infowindow中,我能够从用户的计算机中搜索并选择一个文件.我真的只是寻找JS功能将其发送到我的谷歌驱动器.任何帮助,将不胜感激.

rmn*_*n36 0

您可以找到所需的所有信息,然后在 Google 的 API 参考中找到一些信息,但我已复制了重要的内容以便在下面上传。但是,您肯定需要查看有关身份验证的信息,可以在此链接中找到: https: //developers.google.com/api-client-library/javascript/start/start-js。我还在下面提供了他们的身份验证示例。

<!--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 = '837050751313';

  var apiKey = 'AIzaSyAdjHPT5Pb7Nu56WJ_nlrMGOAgUAtKjiPM';

  var scopes = 'https://www.googleapis.com/auth/plus.me';

  function handleClientLoad() {
    // Step 2: Reference the API key
    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) {
    // Step 3: get authorization to use private data
    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() {
    // Step 4: Load the Google+ API
    gapi.client.load('plus', 'v1').then(function() {
      // Step 5: Assemble the API request
      var request = gapi.client.plus.people.get({
        'userId': 'me'
      });
      // Step 6: Execute the API request
      request.then(function(resp) {
        var heading = document.createElement('h4');
        var image = document.createElement('img');
        image.src = resp.result.image.url;
        heading.appendChild(image);
        heading.appendChild(document.createTextNode(resp.result.displayName));

        document.getElementById('content').appendChild(heading);
      }, function(reason) {
        console.log('Error: ' + reason.result.error.message);
      });
    });
  }
</script>
// Step 1: Load JavaScript client library
<script src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>
Run Code Online (Sandbox Code Playgroud)

您可以通过以下代码设置身份验证来上传文档。您还可以使用“PUT”请求。请参阅此链接了解更多信息:https ://developers.google.com/drive/web/manage-uploads

POST /upload/drive/v2/files?uploadType=media HTTP/1.1
Host: www.googleapis.com
Content-Type: image/jpeg
Content-Length: number_of_bytes_in_file
Authorization: Bearer your_auth_token
Run Code Online (Sandbox Code Playgroud)