Skip to content

Instantly share code, notes, and snippets.

@mikekunze
Last active December 20, 2015 15:39
Show Gist options
  • Save mikekunze/6155928 to your computer and use it in GitHub Desktop.
Save mikekunze/6155928 to your computer and use it in GitHub Desktop.
handler = ()->
document.getElementById("fileField").files
# Make sure a file is selected first
if files.length <= 0
alert('choose a file, first')
return
file = files[0]
fd = new FormData()
fd.append("fileForm", file)
xhr = new XMLHttpRequest()
# define our finish fn
loaded = ()->
alert('finished uploading')
$("#addFile").one "click", handler
xhr.addEventListener 'load', loaded, false
# add progress indicator
xhr.upload.onprogress = (e)->
percent = Math.round((e.loaded * 100) / e.total)
$("#progress").width "#{percent}%"
$("#progressDisplay").html "#{percent}%"
xhr.open "post", "/mUpload"
xhr.send fd
$(document).onReady ()->
$("#addFile").one "click", handler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment