Created
October 2, 2016 22:05
-
-
Save danielwestendorf/69a9ab3b560b9e83266ae448c8a8cfda to your computer and use it in GitHub Desktop.
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
$(document).on('change', 'input[type="file"]', function(delegatedEvent) { | |
var event = delegatedEvent.originalEvent; | |
var csrfToken = $('[name="authenticity_token"]').val(); | |
var presignBaseUrl = $('form').attr('action'); | |
[].forEach.call(event.target.files, function(file) { | |
var filename = encodeURIComponent(file.name); | |
var size = encodeURIComponent(file.size); | |
var uploader = new S3(csrfToken); | |
var presignUrl = presignBaseUrl + | |
'?filename=' + filename + | |
'&size=' + size + | |
'&t=' + Date.now(); | |
uploader.on('upload:success', function(accessUrl) { | |
$('form').append('<p><a target="_blank" href="'+accessUrl+'">'+file.name+'</a></p>'); | |
}); | |
uploader.on('upload:progress', function(event) { | |
var progress = event.loaded / event.total * 100; | |
$('form').append('<div>Progress: '+progress+'</div>') | |
}); | |
uploader.upload(presignUrl, file); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment