-
-
Save djbender/e5733e7ffe1fea411912 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
| $(function() { | |
| $('.directUpload').find("input:file").each(function(i, elem) { | |
| var fileInput = $(elem); | |
| var form = $(fileInput.parents('form:first')); | |
| var submitButton = form.find('input[type="submit"]'); | |
| var progressBar = $("<div class='bar'></div>"); | |
| var barContainer = $("<div class='progress'></div>").append(progressBar); | |
| fileInput.after(barContainer); | |
| fileInput.fileupload({ | |
| fileInput: fileInput, | |
| url: '<%= @s3_direct_post.url %>', | |
| type: 'POST', | |
| autoUpload: true, | |
| formData: <%= @s3_direct_post.fields.to_json.html_safe %>, | |
| paramName: 'file', // S3 does not like nested name fields i.e. name="user[avatar_url]" | |
| dataType: 'XML', // S3 returns XML if success_action_status is set to 201 | |
| replaceFileInput: false, | |
| progressall: function (e, data) { | |
| var progress = parseInt(data.loaded / data.total * 100, 10); | |
| progressBar.css('width', progress + '%') | |
| }, | |
| start: function (e) { | |
| console.log('Started'); | |
| submitButton.prop('disabled', true); | |
| progressBar. | |
| css('background', 'green'). | |
| css('display', 'block'). | |
| css('width', '0%'). | |
| text("Loading..."); | |
| }, | |
| done: function(e, data) { | |
| console.log('Done'); | |
| submitButton.prop('disabled', false); | |
| progressBar.text("Uploading done"); | |
| // extract key and generate URL from response | |
| var key = $(data.jqXHR.responseXML).find("Key").text(); | |
| var url = '//<%= @s3_direct_post.url.host %>/' + key; | |
| // create hidden field | |
| var input = $("<input />", { type:'hidden', name: fileInput.attr('name'), value: url }) | |
| form.append(input); | |
| }, | |
| fail: function(e, data) { | |
| console.log("Failed"); | |
| progressBar. | |
| css("background", "red"). | |
| text("Failed"); | |
| } | |
| }); | |
| }); | |
| }); |
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
| # GET /users/new | |
| def new | |
| @s3_direct_post = S3_BUCKET.presigned_post(key: "uploads/#{SecureRandom.uuid}/${filename}", success_action_status: 201, acl: :public_read) | |
| @user = User.new | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment