Skip to content

Instantly share code, notes, and snippets.

@jlhg
Created May 29, 2018 02:48
Show Gist options
  • Save jlhg/3c06fe640e2b650240fb25541c311dc5 to your computer and use it in GitHub Desktop.
Save jlhg/3c06fe640e2b650240fb25541c311dc5 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title of the document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<form enctype="multipart/form-data">
<input name="file" type="file" />
<input type="button" value="Upload" />
</form>
<progress></progress
</body>
<script>
$(':file').on('change', function() {
var file = this.files[0];
/* if (file.size > 1024) {
* alert('max upload size is 1k')
* }*/
// Also see .name, .type
});
$(':button').on('click', function() {
$.ajax({
// Your server script to process the upload
url: "https://path/to/signed_url",
type: 'PUT',
// Form data
data: new FormData($('form')[0]),
// Tell jQuery not to process data or worry about content-type
// You *must* include these options!
cache: false,
contentType: "image/png",
processData: false,
// Custom XMLHttpRequest
xhr: function() {
var myXhr = $.ajaxSettings.xhr();
if (myXhr.upload) {
// For handling the progress of the upload
myXhr.upload.addEventListener('progress', function(e) {
if (e.lengthComputable) {
$('progress').attr({
value: e.loaded,
max: e.total,
});
}
} , false);
}
return myXhr;
}
});
});
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment