Skip to content

Instantly share code, notes, and snippets.

Created April 9, 2011 18:20
Show Gist options
  • Select an option

  • Save anonymous/911635 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/911635 to your computer and use it in GitHub Desktop.
uploads/uploadify
<%= javascript_include_tag "uploadify/uploaidy.swf", "uploadify/jquery.uploadify.js" %>
<script type="text/javascript">
<%- session_key = Rails.application.config.session_options[:key] -%>
$j(document).ready(function () {
var uploadify_script_data = {};
// Fetch the CSRF meta tag data
var csrf_token = $('meta[name=csrf-token]').attr('content');
var csrf_param = $('meta[name=csrf-param]').attr('content');
// Now associate the data in the config, encoding the data safely
//original method changed by me to that below
//uploadify_script_data[csrf_token] = encodeURI(csrf_param)
uploadify_script_data[csrf_param] = encodeURI(csrf_token)
//another suggested approach if the one above fails is below
//uploadify_script_data[csrf_param] = encodeURI(encodeURIComponent(csrf_token));
// Associate the session information
uploadify_script_data['<%= session_key %>'] = '<%= cookies[session_key] %>';
// Configure Uploadify
$j('#upload_document').click(function(event){
event.preventDefault();
});
$j('#upload-document').uploadify({
uploader : '/uploadify/uploadify.swf',
script : '/uploads/',
scriptData : uploadify_script_data
multi :true
cancelImg : '/uploadify/uploadify-cancel.png',
fileDesc : 'Documents,Photo or Video',
fileExt : '*.mov;*.mp4;*.avi;*.wmv;*.png;*.jpg;*.gif',
auto : true,
sizeLimit : <%= 20.megabytes %>,
width : 150,
height : 25,
hideButton : false,
wmode : 'transparent',
buttonText : 'Upload',
queueID : 'noQueueForMePlease',
onProgress :
//Function 'onComplete' below will process response from pictures_controller 'create'
//format.json{render :json => { :result => 'success', :picture => admin_picture_path(@picture) } }
onComplete : function(event, queueID, fileObj, response, data) {
var data = eval('(' + response + ')');
if(data.result == 'success'){
$.getScript(data.upload);
}
else{
alert(data.error);
//We can have a <hr/> before alert or notice using jquery
$('#alert').html(data.error)
}
},
});
$j('#upload_submit').click(function(event){
event.preventDefault();
$j('#upload_document').uploadifyUpload();
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment