上传到Google云端硬盘并显示进度百分比

时间:2015-04-14 15:55:54

标签: javascript php google-drive-api

我使用以下代码将大型文件上传到Google云端硬盘。如何以数字百分比显示进度?

我不想显示进度条,只有百分比如" 20%"。

server.gs

function doGet(e) {
  return HtmlService.createHtmlOutputFromFile('upload.html');
}

function uploadFiles(form) {

  try {

    var dropbox = "uploads";
    var folder, folders = DriveApp.getFoldersByName(dropbox);

    if (folders.hasNext()) {
      folder = folders.next();
    } else {
      folder = DriveApp.createFolder(dropbox);
    }

    var blob = form.myFile;    
    var file = folder.createFile(blob);    
    file.setDescription("VideoName_Number: " + form.myName);

    return "Episode uploaded successfully " + file.getUrl();

  } catch (error) {

    return error.toString();
  }

}

upload.html

<div>
  <form id="myForm">
      <input type="text" name="myName" placeholder="VideoName_Number">
      <input type="file" name="myFile">
      <input type="submit" value="Upload Anime" 
             onclick="this.value='Uploading..';
                      google.script.run.withSuccessHandler(fileUploaded)
                      .uploadFiles(this.parentNode);
                      return false;">
  </form>

  <div id="output"></div>

  <script>
      function fileUploaded(status) {
          document.getElementById('myForm').style.display = 'none';
          document.getElementById('output').innerHTML = status;
      }
  </script>

  <style>
   input { display:block; margin: 20px; }
  </style>
</div>

1 个答案:

答案 0 :(得分:1)

有一篇关于如何仅使用纯javascript使用google drive api的完整文章。本文使用称为MediaUploader的外部文件(Jquery)。你可以在这里测试一个快速演示。

Demo of the google drive using javascript completely