Created
August 1, 2014 01:38
-
-
Save cgrusden/fd641e65d00e4905c698 to your computer and use it in GitHub Desktop.
Ajax File Upload
This file contains hidden or 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
$('input#headshot').on('change', (event) -> | |
jQuery.active++ | |
files = event.target.files | |
formData = new FormData() | |
for file in files | |
formData.append('images[]', file, file.name) | |
xhr = new XMLHttpRequest() | |
xhr.open('POST', $(this).closest('form').attr('action'), true) | |
xhr.setRequestHeader('X-CSRF-Token', $('input[name="authenticity_token"]').attr('value')) | |
xhr.onreadystatechange = -> | |
if (xhr.readyState == 4 && xhr.status == 200) | |
eval(xhr.responseText) | |
jQuery.active-- | |
xhr.send(formData) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment