Created
August 25, 2014 05:23
-
-
Save rankjie/7fd60376d88c362b00bc to your computer and use it in GitHub Desktop.
Upload file with XHR
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
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