Firebase云存储上载进度(<256kb)

时间:2019-03-15 02:43:56

标签: firebase google-cloud-storage firebase-storage

假设将this作为要上传的代码。

// File or Blob named mountains.jpg
var file = ...

// Create the file metadata
var metadata = {
  contentType: 'image/jpeg'
};

// Upload file and metadata to the object 'images/mountains.jpg'
var uploadTask = storageRef.child('images/' + file.name).put(file, metadata);

// Listen for state changes, errors, and completion of the upload.
uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED, // or 'state_changed'
  function(snapshot) {
    // Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
    var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
    console.log('Upload is ' + progress + '% done');
    switch (snapshot.state) {
      case firebase.storage.TaskState.PAUSED: // or 'paused'
        console.log('Upload is paused');
        break;
      case firebase.storage.TaskState.RUNNING: // or 'running'
        console.log('Upload is running');
        break;
    }
  }, function(error) {

  // A full list of error codes is available at
  // https://firebase.google.com/docs/storage/web/handle-errors
  switch (error.code) {
    case 'storage/unauthorized':
      // User doesn't have permission to access the object
      break;

    case 'storage/canceled':
      // User canceled the upload
      break;

    ...

    case 'storage/unknown':
      // Unknown error occurred, inspect error.serverResponse
      break;
  }
}, function() {
  // Upload completed successfully, now we can get the download URL
  uploadTask.snapshot.ref.getDownloadURL().then(function(downloadURL) {
    console.log('File available at', downloadURL);
  });
});

在以下情况下如何指示进度(使用控制台)

  • 您的用户的互联网连接速度缓慢(<50kb / s)

  • 上传小文件(<.5mb)

'state_changed'事件似乎为250kb的进度块触发。在官方docs中找不到与此相关的任何内容。

我可以以某种方式减小块大小或每秒进行定期更新吗?

任何其他解决方法都受到高度赞赏。

P.S .:每个用户的连接都会有所不同,我不会假设50kb / s,而只是根据假设显示静态进度。

0 个答案:

没有答案