Skip to content

Instantly share code, notes, and snippets.

@nelix
Created September 12, 2013 03:53
Show Gist options
  • Select an option

  • Save nelix/6532868 to your computer and use it in GitHub Desktop.

Select an option

Save nelix/6532868 to your computer and use it in GitHub Desktop.
some stuff.
$(document).ready( function() {
var form = $('#fileUpload'),
progress = $('#progress'),
data = new FormData();
form.submit(function(event) {
event.preventDefault();
var file = $('#file')[0];
progress.text('Starting to upload file:' + file.files[0].name);
data.append('key', '<%= @key %>/' + file.files[0].name);
data.append('AWSAccessKeyId', '<%= @id %>');
data.append('acl', 'public-read');
data.append('policy', '<%= @policy %>');
data.append('signature','<%= @signature %>');
data.append('file', file.files[0]);
console.log('submit', data);
$.ajax( {
processData: false,
contentType: false,
type: 'POST',
url: '<%= AWS_HOST %>',
data: data,
xhrFields: {
withCredentials: true
},
dataType: 'xml',
success: function(response) {
progress.css('width', '0%');
progress.text('File upload done. Sending callback.');
console.log(response);
$.ajax( {
type: 'POST',
url: '<%= @callback %>',
dataType: 'json',
data: {upload: {filename: file.files[0].name, key: '<%= @key %>'}},
success: function(response) {
progress.text('Finished.');
console.log(response);
form.hide();
}
});
},
xhr: function() {
// Patch in some progress
var xhr = $.ajaxSettings.xhr();
if(xhr.upload){
xhr.upload.addEventListener('progress', function(event) {
if (event.lengthComputable) {
var percentComplete = parseInt(event.loaded / event.total * 100, 10);
console.log(percentComplete)
progress.text('File upload ' + percentComplete + '%');
progress.css('width', percentComplete + '%');
}
}, false);
}
return xhr;
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment