Created
June 13, 2012 09:37
-
-
Save hugowan/2923089 to your computer and use it in GitHub Desktop.
HTML5 Ajax upload
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
<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