简体   繁体   English

跟踪通过js将文件上传到Google云存储的进度?

[英]Track progress of file upload to google cloud storage via js?

I'm wondering if it is possible to track the progress of a file upload to google cloud storage when using the javascript client? 我想知道使用JavaScript客户端时是否可以跟踪文件上传到Google云存储的进度? As I have a progress bar that I would like to show to the user on how much of the file has been uploaded so far. 我有一个进度条,希望向用户显示到目前为止已上传了多少文件。

I'm using the same upload code that google gives on their example page for file upload. 我使用的是Google在其示例页面上提供的文件上传代码。

Instead of using the gapi.client.request api provided by the js library I instead created an XMLHttpRequest and appended a event listener for the event progress . 我没有使用js库提供的gapi.client.request api,而是创建了XMLHttpRequest并为事件progress附加了事件侦听器。 As seen by the following: 如以下所示:

      const boundary = '-------314159265358979323846';
      const delimiter = "\r\n--" + boundary + "\r\n";
      const close_delim = "\r\n--" + boundary + "--";

      var reader = new FileReader();
      reader.readAsBinaryString(fileData);
      reader.onload = function(e) {
        var contentType = fileData.type || 'application/octet-stream';
        var metadata = {
          'name': 'testing',
          'mimeType': contentType
        };

        var base64Data = btoa(reader.result);
        var multipartRequestBody =
          delimiter +
          'Content-Type: application/json\r\n\r\n' +
          JSON.stringify(metadata) +
          delimiter +
          'Content-Type: ' + contentType + '\r\n' +
          'Content-Transfer-Encoding: base64\r\n' +
          '\r\n' +
          base64Data +
          close_delim;

        gapi.client.request()
        var xhr = new XMLHttpRequest();

        xhr.open('POST', 'https://www.googleapis.com/upload/storage/v1/b/bucket/o?uploadType=multipart&key=apiKey&alt=json', true);

        xhr.setRequestHeader('Content-Type', 'multipart/mixed; boundary="' + boundary + '"');
        xhr.setRequestHeader('authorization', 'Bearer ' + gapi.auth.getToken().access_token);

        xhr.upload.addEventListener("progress", function(e) {
          var pc = parseFloat(e.loaded / e.total * 100).toFixed(2);
        }, false);

        xhr.onreadystatechange = function(e) {
          if (xhr.readyState == 4) {
            console.log(xhr.responseText);
          }
        };

        xhr.send(multipartRequestBody);

Attribution: Majority of code was taken from Google's js library with the only addition being the event listener for listening to the progress of the upload. 来源:大部分代码来自Google的js库,唯一的增加是事件侦听器,用于侦听上传进度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Node.js 将 pdf 文件上传到 Google Cloud Storage - Upload a pdf file to Google Cloud Storage with Node.js Google Cloud Functions - 通过 HTTP 上传到 Google Cloud Storage - Google Cloud Functions - Upload to Google Cloud Storage via HTTP 如何通过NodeJS将文件上传到S3并跟踪浏览器的进度? - How to upload a file to S3 via NodeJS and track progress in the browser? 通过javascript将文件上传到Google云存储 - Upload file to google cloud storage by javascript 创建上传文件到谷歌云存储 - Create an upload file to google cloud storage 使用SignedUrl将文件上传到Google Cloud Storage - File upload to Google Cloud Storage with SignedUrl 上传文件到谷歌云存储使用链接 - Upload file to google cloud storage use link 使用可恢复上传 url 在 React js 中上传文件在谷歌云存储中失败,抛出 CORS 策略错误 - Uploading file in react js with resumable upload url is failing in google cloud storage, throwing CORS policy error 使用节点js创建预签名网址并将文件上传到Google云端存储 - create a pre signed url and upload a file to google cloud storage using node js 如何使用 Google Cloud Storage JSON API 获取可恢复上传的上传进度信息? - How to get upload progress informations for a resumable-upload with Google Cloud Storage JSON API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM