Skip to content

Instantly share code, notes, and snippets.

@michelson
Created July 7, 2011 21:39
Show Gist options
  • Save michelson/1070610 to your computer and use it in GitHub Desktop.
Save michelson/1070610 to your computer and use it in GitHub Desktop.
plup
Uploadizr = $.klass({
initialize: function(submiter , path ){
//console.log("picker!! " +submiter+" "+$(this.element).attr("token")+" "+path);
var current_id = "";
var token = $(this.element).attr("token")
var uploader = new plupload.Uploader({
runtimes : "html5",
browse_button : $(this.element).attr("id"),
max_file_size : '10mb',
url : path,
multipart: true,
unique_names : true
// filters : [
// {title : "Image files", extensions : "jpg,gif,png"},
// {title : "Zip files", extensions : "zip"}
// ],
// multipart_params: {
// "authenticity_token" : $(this.element).attr("token"),
// "plupload_tmp_id" : current_id
// }
});
var maxfiles = 3;
// Bind it after .init default events are bound inside the init call.
uploader.init();
uploader.bind('FilesAdded',function(up,files) {
// console.log('Selected files: ' + files.length);
// console.log('Files in uploader: ' + up.files);
if(up.total.queued > maxfiles ) {
alert("Maximum number of files you can upload is "+maxfiles+". Please narrow your selection to "+maxfiles+" or less.\nSystem will remove extra files.")
// search files to delete
if( up.state != 2){
$.each(files, function(i, file) {
if(up.files.length > maxfiles){
up.removeFile(file);
}else{
$( "<tr id='asset-row-"+file.id+"'>" +
"<td>" + file.name + " (" + plupload.formatSize(file.size) + ") <b> </b> </td>" +
"</tr>" ).insertAfter('#assets-rows');
}
});
}
// console.log("queued"+up.total.queued);
}else{
if( up.state != 2){
$.each(files, function(i, file) {
$( "<tr id='asset-row-"+file.id+"'>" +
"<td>" + file.name + " (" + plupload.formatSize(file.size) + ") <b> </b> </td>" +
"</tr>" ).insertAfter('#assets-rows');
});
}
}
// console.log(up);
// now iterate the allowed files
up.refresh();
});
uploader.bind('QueueChanged', function(up) {
//console.log('Queued Changed: ' + uploader.total.queued);
});
uploader.bind("FilesRemoved", function(up, files) {
// console.log('removed files: ' + files);
});
uploader.bind("FileUploaded", function(up, file , response) {
//console.log('uploaded File: ');
//console.log(file);
eval(response.response);
});
uploader.bind('UploadProgress', function(up, file) {
// console.log(file.percent + "%");
//console.log(uploader.total.queued + "%");
$('#loaded span#load').html(uploader.total.loaded + "kb");
$('#loaded span#size').html(uploader.total.size + "kb");
$('#loaded span#percent').html(uploader.total.percent + "%");
//$('#queued_files span#queued').html(uploader.total.queued);
$('#queued_files span#uploaded').html(uploader.total.uploaded + 1);
$('#failed span').html(uploader.total.failed);
$('#asset-row-' + file.id + " b").html(file.percent + "%");
});
uploader.bind("Error", function(up , error){
// console.log("errors! in:");
// console.log(error);
})
uploader.bind("BeforeUpload", function(up, file){
// console.log(up);
// add settings dynamic
up.settings.multipart_params = {
authenticity_token: token,
plupload_tmp_id: file.id
};
// current_id = file.id;
})
$(submiter).click(function(e) {
//console.log(uploader);
uploader.start();
e.preventDefault();
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment