Created
May 20, 2015 21:14
-
-
Save kahdojay/61007d041318ace46a8b to your computer and use it in GitHub Desktop.
This file contains 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, | |
done: function(e, data) { | |
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); | |
} | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment