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
formData
class @JackUp.FileUploader
constructor: (@options) ->
@path = @options.path
@responded = false
_onProgressHandler: (file) =>
(progress) =>
if progress.lengthComputable
percent = progress.loaded/progress.total*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 event.target.readyState != 4
try
status = event.target.status
catch error
return
acceptableStatuses = [200, 201]
acceptableStatus = acceptableStatuses.indexOf(status) > -1
if status > 0 && !acceptableStatus
@trigger 'upload:failure', responseText: event.target.responseText, event: event, file: file
if acceptableStatus && event.target.responseText && !@responded
@responded = true
@trigger 'upload:success', responseText: event.target.responseText, event: event, file: file
upload: (file) ->
xhr = new XMLHttpRequest()
xhr.upload.addEventListener 'progress', @_onProgressHandler(file), false
xhr.addEventListener 'readystatechange', @_onReadyStateChangeHandler(file), false
xhr.open '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.