Skip to content

Instantly share code, notes, and snippets.

@TowardsDeath
Created August 15, 2012 18:42
Show Gist options
  • Save TowardsDeath/3362263 to your computer and use it in GitHub Desktop.
Save TowardsDeath/3362263 to your computer and use it in GitHub Desktop.
jQuery progress handler
$.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