0

I am trying to get canceling all uploads to work with client size resizing. I can get one or the other to work, but not both at the same time. I have been maintaining a stack of xhr objects in my add method, but that requires calling data,submit() directly. I have now changed my code to hook into fileuploadadd and the resizing works great, but canceling only cancels one file (I assume the latest one).

This is what it currently looks like:

@addFile = (e, data) =>
  if @validate(data)
    this.transfers.push(data)
  else
    $result = $("<div class='error'>#{data.files[0].name} failed to upload. Files must be 5MB or less, and must be gif, jpg, or png format.</div>")
    $messageContainer.append($result)
    e.preventDefault()

$(input).fileupload
  disableImageResize: /Android(?!.*Chrome)|Opera/.test(window.navigator && navigator.userAgent)
  imageMaxWidth: 1024
  imageMaxHeight: 1024
  imageCrop: false
.bind('fileuploadadd', @addFile)
Brandon
  • 1,735
  • 2
  • 22
  • 37

1 Answers1

0

Thanks to dannio and his post I was able to solve this. Here is what I ended up with and it works great:

class Uploader
  transfers: []
  cancelAll: ->
    for xhr in this.transfers
      xhr.abort() if xhr
    this.transfers = []
  configure: (input) ->
    configurator = this

    $(input).fileupload
      disableImageResize: /Android(?!.*Chrome)|Opera/.test(window.navigator && navigator.userAgent)
      imageMaxWidth: 1024
      imageMaxHeight: 1024
      imageCrop: false
      add: (e, data) =>
        if this.validate(data)
          $target = $(e.target)
          data.process(->
            return $target.fileupload('process', data)
          ).done(->
            configurator.transfers.push data.submit()
          )
  validate: (data) ->
    if typeof data.files[0].type != 'undefined' && data.files[0].size != 'undefined'
      valid = data.files[0].type in ["image/jpeg", "image/png", "image/gif"]
      valid && data.files[0].size <= 5242880
    else
      true
Community
  • 1
  • 1
Brandon
  • 1,735
  • 2
  • 22
  • 37