Skip to content

Instantly share code, notes, and snippets.

@cgrusden
Created August 1, 2014 01:38
Show Gist options
  • Save cgrusden/fd641e65d00e4905c698 to your computer and use it in GitHub Desktop.
Save cgrusden/fd641e65d00e4905c698 to your computer and use it in GitHub Desktop.
Ajax File Upload
$('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