Created
August 30, 2015 06:57
-
-
Save morshedalam/27a9ac2f87278f9d311b 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
var initializeUploaderWithFiles = function (file_types) { | |
var url = $('#fileupload').attr('action'), | |
uploadButton = $('<button/>') | |
.addClass('btn btn-primary') | |
.prop('disabled', true) | |
.text('Processing...') | |
.on('click', function () { | |
var $this = $(this), | |
data = $this.data(); | |
$this | |
.off('click') | |
.text('Abort') | |
.on('click', function () { | |
$this.remove(); | |
data.abort(); | |
}); | |
data.submit().always(function () { | |
$this.remove(); | |
}); | |
}), | |
accept_exp = new RegExp("(\.|\/)(" + file_types + ")$", "i"), | |
fileUploadErrors = { | |
maxFileSize: 'File is too big', | |
minFileSize: 'File is too small', | |
acceptFileTypes: 'Filetype not allowed', | |
maxNumberOfFiles: 'Max number of files exceeded', | |
uploadedBytes: 'Uploaded bytes exceed file size', | |
emptyResult: 'Empty file upload result' | |
}; | |
$('#fileupload').fileupload({ | |
url: url, | |
dataType: 'json', | |
autoUpload: false, | |
acceptFileTypes: accept_exp, | |
//maxFileSize: 999000, | |
// Enable image resizing, except for Android and Opera, | |
// which actually support image resizing, but fail to | |
// send Blob objects via XHR requests: | |
disableImageResize: /Android(?!.*Chrome)|Opera/.test(window.navigator.userAgent), | |
previewMaxWidth: 64, | |
previewMaxHeight: 64, | |
previewCrop: true | |
}).on('fileuploadadd', function (e, data) { | |
data.context = $('<div/>').appendTo('#files'); | |
$.each(data.files, function (index, file) { | |
var node = $('<p/>') | |
.append($('<span/>').text(file.name)); | |
if (!index) { | |
node | |
.append('<br>') | |
.append(uploadButton.clone(true).data(data)); | |
} | |
node.appendTo(data.context); | |
}); | |
}).on('fileuploadprocessalways', function (e, data) { | |
var index = data.index, | |
file = data.files[index], | |
node = $(data.context.children()[index]); | |
if (file.preview) { | |
node | |
//.prepend('<br>') | |
.prepend(file.preview); | |
} | |
if (file.error) { | |
node | |
.append('<br>') | |
.append($('<span class="text-danger"/>').text(file.error)); | |
} | |
if (index + 1 === data.files.length) { | |
data.context.find('button') | |
.text('Upload') | |
.prop('disabled', !!data.files.error); | |
} | |
}).on('fileuploadprogressall', function (e, data) { | |
console.log('fileuploadprogressall'); | |
var progress = parseInt(data.loaded / data.total * 100, 10); | |
$('#progress .progress-bar').css( | |
'width', | |
progress + '%' | |
); | |
}).on('fileuploaddone', function (e, data) { | |
console.log('fileuploaddone'); | |
$.each(data.result.files, function (index, file) { | |
if (file.url) { | |
var link = $('<a>') | |
.attr('target', '_blank') | |
.prop('href', file.url); | |
$(data.context.children()[index]) | |
.wrap(link); | |
} else if (file.error) { | |
var error = $('<span class="text-danger"/>').text(file.error); | |
$(data.context.children()[index]) | |
.append('<br>') | |
.append(error); | |
} | |
}); | |
}).on('fileuploadfail', function (e, data) { | |
console.log('fileuploadfail'); | |
$.each(data.files, function (index) { | |
var error = $('<span class="text-danger"/>').text('File upload failed.'); | |
$(data.context.children()[index]) | |
.append('<br>') | |
.append(error); | |
}); | |
}).prop('disabled', !$.support.fileInput) | |
.parent().addClass($.support.fileInput ? undefined : 'disabled'); | |
} | |
<%= form_for file_object, :url => [uploadable, file_object], :html => {:multipart => true, :id => "fileupload"} do |f| %> | |
<div class="row fileupload-buttonbar"> | |
<div class="col-lg-7"> | |
<span class="btn btn-success fileinput-button"> | |
<a class="fa fa-plus white"></a> | |
<span> | |
<%= t('fileupload.add_files') %> | |
</span> | |
<%= f.file_field :file %> | |
</span> | |
<%= link_to 'Cancel', [uploadable, file_object], :class => 'btn btn-default btn-cancel' %> | |
</div> | |
<div class="col-lg-5 fileupload-progress fade"> | |
<div aria-valuemax="100" aria-valuemin="0" class="progress progress-striped active" role="progressbar"> | |
<div class="progress-bar progress-bar-success" style="width:0%;"></div> | |
</div> | |
<div class="progress-extended"> </div> | |
</div> | |
</div> | |
<table class="table table-striped" role="presentation"> | |
<tbody class="files"></tbody> | |
</table> | |
<% end %> | |
<script id="template-upload" type="text/x-tmpl"> | |
{% for (var i=0, file; file=o.files[i]; i++) { %} | |
<tr class="template-upload fade"> | |
<td> | |
<span class="preview"></span> | |
</td> | |
<td> | |
<p class="name">{%=file.name%}</p> | |
<strong class="error text-danger"></strong> | |
</td> | |
<td> | |
<p class="size">Processing...</p> | |
<div class="progress progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0"><div class="progress-bar progress-bar-success" style="width:0%;"></div></div> | |
</td> | |
<td> | |
{% if (!i && !o.options.autoUpload) { %} | |
<button class="btn btn-primary start btn-xs" disabled> | |
<i class="glyphicon glyphicon-upload"></i> | |
<span>Start</span> | |
</button> | |
{% } %} | |
{% if (!i) { %} | |
<button class="btn btn-warning cancel btn-xs"> | |
<i class="glyphicon glyphicon-ban-circle"></i> | |
<span>Cancel</span> | |
</button> | |
{% } %} | |
</td> | |
</tr> | |
{% } %} | |
</script> | |
<!-- The template to display files available for download --> | |
<script id="template-download" type="text/x-tmpl"> | |
{% for (var i=0, file; file=o.files[i]; i++) { %} | |
<tr class="template-download fade"> | |
<td> | |
<span class="preview"> | |
{% if (file.thumb_url) { %} | |
<a href="{%=file.full_path%}" title="{%=file.name%}" download="{%=file.name%}" data-gallery><img src="{%=file.thumb_url%}"></a> | |
{% } %} | |
</span> | |
</td> | |
<td> | |
<p class="name"> | |
{% if (file.url) { %} | |
<a href="{%=file.url%}" title="{%=file.name%}" download="{%=file.name%}" {%=file.thumbnailUrl?'data-gallery':''%}>{%=file.name%}</a> | |
{% } else { %} | |
<span>{%=file.name%}</span> | |
{% } %} | |
</p> | |
{% if (file.error) { %} | |
<div><span class="label label-danger">Error</span> {%=file.error%}</div> | |
{% } %} | |
</td> | |
<td> | |
<span class="size">{%=o.formatFileSize(file.size)%}</span> | |
</td> | |
<td> | |
{% if (file.delete_url) { %} | |
<button class="btn btn-danger delete btn-xs" data-type="{%=file.delete_type%}" data-url="{%=file.delete_url%}"{% if (file.deleteWithCredentials) { %} data-xhr-fields='{"withCredentials":true}'{% } %}> | |
<i class="glyphicon glyphicon-trash"></i> | |
<span>Delete</span> | |
</button> | |
{% } else { %} | |
<button class="btn btn-warning cancel btn-xs"> | |
<i class="glyphicon glyphicon-ban-circle"></i> | |
<span>Cancel</span> | |
</button> | |
{% } %} | |
</td> | |
</tr> | |
{% } %} | |
</script> | |
<% content_for(:page_bottom) do %> | |
<script type="text/javascript"> | |
initializeUploaderWithFiles("<%=file_object.file.extension_white_list.join('|')%>"); | |
</script> | |
<% end %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment