账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    在第三方js库中如何使用rails active_storage获得上传进度
    34
    0

    rails文件直传说明

    Direct Uploads教程

    我的用法

    import { DirectUpload } from 'activestorage'
    
    uploadFile (file) {
            // activestorage: direct_upload
            let vm = this
            const url = '/rails/active_storage/direct_uploads'
            const upload = new DirectUpload(file, url, upload)
    
            return new Promise((resolve, reject) => {
              upload.create((error, blob) => {
                if (error) {
                  console.log(error)
                  vm.$Message.error(error)
                } else {
                  // Add an appropriately-named hidden input to the form with a value of blob.signed_id
                  console.log(blob)
                  resolve(blob.signed_id)
                }
              })
            })
          }

    需求

    希望加上上传进度提示,因此需要获取上传进度

    问题

    教程示例为:

    import { DirectUpload } from "activestorage"
     
    class Uploader {
      constructor(file, url) {
        this.upload = new DirectUpload(this.file, this.url, this)
      }
     
      upload(file) {
        this.upload.create((error, blob) => {
          if (error) {
            // Handle the error
          } else {
            // Add an appropriately-named hidden input to the form
            // with a value of blob.signed_id
          }
        })
      }
     
      directUploadWillStoreFileWithXHR(request) {
        request.upload.addEventListener("progress",
          event => this.directUploadDidProgress(event))
      }
     
      directUploadDidProgress(event) {
        // Use event.loaded and event.total to update the progress bar
      }
    }

    源码:https://github.com/rails/rail...

    DirectUpload的实例不是应该能访问directUploadWillStoreFileWithXHR, directUploadDidProgress方法吗,但是我这么调用会报错没有该方法,求搭救

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部