Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
61 lines (45 sloc) 1.78 KB
railsFormData = ->
csrfParam = $('meta[name=csrf-param]').attr('content')
csrfToken = $('meta[name=csrf-token]').attr('content')
formData = new FormData()
formData.append(csrfParam, csrfToken)
#return the FormData object
class @JackUp.FileUploader
constructor: (@options) ->
@path = @options.path
@responded = false
_onProgressHandler: (file) =>
(progress) =>
if progress.lengthComputable
percent = progress.loaded/*100
@trigger 'upload:percentComplete', percentComplete: percent, progress: progress, file: file
if percent == 100
@trigger 'upload:sentToServer', file: file
_onReadyStateChangeHandler: (file) =>
(event) =>
status = null
return if != 4
status =
catch error
acceptableStatuses = [200, 201]
acceptableStatus = acceptableStatuses.indexOf(status) > -1
if status > 0 && !acceptableStatus
@trigger 'upload:failure', responseText:, event: event, file: file
if acceptableStatus && && !@responded
@responded = true
@trigger 'upload:success', responseText:, event: event, file: file
upload: (file) ->
xhr = new XMLHttpRequest()
xhr.upload.addEventListener 'progress', @_onProgressHandler(file), false
xhr.addEventListener 'readystatechange', @_onReadyStateChangeHandler(file), false 'POST', @path, true
formData = railsFormData()
#append the file
#we should probably make the field name configurable
formData.append('file', file)
@trigger 'upload:start', file: file
xhr.send formData
_.extend JackUp.FileUploader.prototype, JackUp.Events
Jump to Line
Something went wrong with that request. Please try again.