Sha256: 37163c94f4e8a4b675fa41b9d890ff1a2879ecd8c3ebb6a72ea08853dd42cca9

Contents?: true

Size: 868 Bytes

Versions: 8

Compression:

Stored size: 868 Bytes

Contents

export class BlobUpload {
  constructor(blob) {
    this.blob = blob
    this.file = blob.file

    const { url, headers } = blob.directUploadData

    this.xhr = new XMLHttpRequest
    this.xhr.open("PUT", url, true)
    for (const key in headers) {
      this.xhr.setRequestHeader(key, headers[key])
    }
    this.xhr.addEventListener("load", event => this.requestDidLoad(event))
    this.xhr.addEventListener("error", event => this.requestDidError(event))
  }

  create(callback) {
    this.callback = callback
    this.xhr.send(this.file)
  }

  requestDidLoad(event) {
    const { status, response } = this.xhr
    if (status >= 200 && status < 300) {
      this.callback(null, response)
    } else {
      this.requestDidError(event)
    }
  }

  requestDidError(event) {
    this.callback(`Error storing "${this.file.name}". Status: ${this.xhr.status}`)
  }
}

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
activestorage_legacy-0.2.1 app/javascript/activestorage/blob_upload.js
activestorage_legacy-0.2.0 app/javascript/activestorage/blob_upload.js
activestorage_legacy-0.1.3 app/javascript/activestorage/blob_upload.js
activestorage_legacy-0.1.2 app/javascript/activestorage/blob_upload.js
activestorage_legacy-0.1.1 app/javascript/activestorage/blob_upload.js
activestorage_legacy-0.1.1.alpha app/javascript/activestorage/blob_upload.js
activestorage_legacy-0.1 app/javascript/activestorage/blob_upload.js
ruby-on-quails-0.1.0 activestorage/app/javascript/activestorage/blob_upload.js