Last active
August 29, 2015 14:14
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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