Skip to content

Instantly share code, notes, and snippets.

@hugowan
Created June 13, 2012 09:37
Show Gist options
  • Save hugowan/2923089 to your computer and use it in GitHub Desktop.
Save hugowan/2923089 to your computer and use it in GitHub Desktop.
HTML5 Ajax upload
<form>
<input type="file" id="file" name="file">
<input type="submit">
</form>
<script>
$('form').submit(function (e) {
e.preventDefault();
var data;
data = new FormData();
data.append( 'file', $( '#file' )[0].files[0] );
$.ajax({
url: 'http://www.xxx.com/upload.php',
data: data,
processData: false,
contentType: false,
type: 'POST',
xhr: function(){
var xhr = $.ajaxSettings.xhr();
xhr.upload.addEventListener('progress', function(evt) {
var percent = evt.loaded/evt.total*100;
console.log('Upload progress: ' + percent + '%');
}, false);
return xhr;
},
success: function(data){
alert(data);
}
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment