Created
August 15, 2012 18:42
-
-
Save TowardsDeath/3362263 to your computer and use it in GitHub Desktop.
jQuery progress handler
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
$.ajax({ | |
type: 'POST', | |
url: '/files/upload', | |
/* jQuery automatically processes and transforms Ajax data into a querystring. | |
* It should not do that with our binary data. */ | |
processData: false, | |
data: file, | |
/* The 'upload' object and onprogress event are part of the new XmlHttpRequest (XHR) 2. | |
* With jQuery v1.7.2 there's no direct way to access this event, | |
* so we need to manually access the core XmlHttpRequest object to attach an event handler. | |
* | |
* 15 aug 2012: XHR 2 is still relatively new, so be sure to check which browsers you intend to support. | |
* For example, Internet Explorer doesn't support it in version 9 and below. | |
* For a full support chart, see http://caniuse.com/xhr2 */ | |
xhr: function() { | |
extendedXhr = $.ajaxSettings.xhr(); | |
if( extendedXhr.upload ){ | |
extendedXhr.upload.onprogress = function( e ){ | |
/* Only handle progress if the currently uploaded size is known */ | |
if( !e.lengthComputable ) return; | |
console.log( e.loaded / e.total ); | |
} | |
} | |
return extendedXhr; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment