Skip to content

Instantly share code, notes, and snippets.

@RoxasShadow
Last active August 29, 2015 14:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RoxasShadow/8fefee331b6bab2c9324 to your computer and use it in GitHub Desktop.
Save RoxasShadow/8fefee331b6bab2c9324 to your computer and use it in GitHub Desktop.
Let's say you want to send to your server a modified version of the file the user is going to upload.
listeners: () =>
@dropzone.on 'addedfile', (file) =>
_reader = new FileReader
_reader.onload = (e) =>
_attachment = _reader.result
@attachmentsQueue.push { file: file, content: _attachment }
_reader.readAsBinaryString file
sendAttachment: (attachment, file) =>
_blob = new Blob [attachment], { type: file.type }
_formData = new FormData
_formData.append 'message[attachments_attributes][][file]', _blob, file.name
_progressBar = $('[data-dz-uploadprogress]')
$.ajax
url: "/api/v1/messages/#{@id}"
method: 'PUT'
cache: false
contentType: false
processData: false
data: _formData
beforeSend: () =>
_progressBar.css 'width', '0'
xhr: () =>
_progress = (e) =>
if e.lengthComputable
_percentComplete = (e.loaded / e.total) * 100
_progressBar.css 'width', "#{_percentComplete}%"
_xhr = new window.XMLHttpRequest
_xhr.upload.addEventListener 'progress', _progress, false
_xhr.addEventListener 'progress', _progress, false
return _xhr
.done (data) =>
log 'UPLOADED SUCCESS', 'success'
@onUploadSuccess file, data
@processAttachmentsQueue()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment