Skip to content

Instantly share code, notes, and snippets.

@rankjie
Created August 25, 2014 05:23
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 rankjie/7fd60376d88c362b00bc to your computer and use it in GitHub Desktop.
Save rankjie/7fd60376d88c362b00bc to your computer and use it in GitHub Desktop.
Upload file with XHR
xhr = new XMLHttpRequest()
# 进度
xhr.upload.addEventListener "progress", uploadProgress, false
# 完成
xhr.addEventListener "load", uploadComplete, false
xhr.addEventListener "error", console.log, false
xhr.addEventListener "abort", console.log, false
fd = new FormData()
fd.append "file", file
uploadPoint = "/file_upload"
xhr.open "POST", uploadPoint, true
xhr.send fd
uploadProgress = (evt) ->
return progress = Math.round(evt.loaded * 100 / evt.total) if evt.lengthComputable?
progress = "无法计算进度..."
# 上传完毕
uploadComplete = (evt) ->
# do wtf you want.
console.log evt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment