Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.     <title>Document</title>
  8. </head>
  9. <body>
  10.     <input type="file" onchange="uploadFileGoogleDrive(event)">
  11.    
  12.     <div class="progress">0%</div>
  13.    
  14.     <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
  15.     <script>
  16. function uploadFileGoogleDrive(event) {
  17.    
  18.     let file= event.target.files[0];
  19.     var reader = new FileReader();
  20.  
  21.     reader.readAsBinaryString(file);
  22.     reader.onload = function (e) {
  23.         let multipart =
  24. `
  25. --foo
  26. Content-Type: application/json; charset=UTF-8
  27.  
  28. {
  29.     "name": "${file.name}"
  30. }
  31.  
  32. --foo
  33. Content-Transfer-Encoding: base64
  34.  
  35. ${btoa(reader.result)}
  36. --foo--`;
  37.         let ajax = $.ajax({
  38.             url: 'https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&fields=*',
  39.             xhr: function () {
  40.                 var xhr = new window.XMLHttpRequest();
  41.                 xhr.upload.addEventListener("progress", function (evt) {
  42.                     if (evt.lengthComputable) {
  43.                         let percentComplete = (evt.loaded / evt.total) * 100;
  44.                         $('.progress').text(percentComplete + '%');
  45.                     }
  46.                 }, false);
  47.  
  48.                 return xhr;
  49.             },
  50.             headers: {
  51.                 "Authorization": "Bearer <token>",
  52.                 "Content-Type": "multipart/related; boundary=foo",
  53.             },
  54.             type: 'POST',
  55.             data: multipart,
  56.             success: function (result, textStatus, xhr) {
  57.                 if (xhr.status == 200) {
  58.                       console.log(result);
  59.                 }
  60.             },
  61.             error: function (results) {
  62.                
  63.             }
  64.         });
  65.        
  66.     }
  67.  
  68. }
  69.  
  70.     </script>
  71. </body>
  72. </html>