[241] | 1 |
|
---|
| 2 | function upload_init(){
|
---|
| 3 | var ul = $('#upload ul');
|
---|
| 4 |
|
---|
| 5 | $('#drop a').click(function(){
|
---|
| 6 | // Simulate a click on the file input button
|
---|
| 7 | // to show the file browser dialog
|
---|
| 8 |
|
---|
| 9 | $(this).parent().find('input').click();
|
---|
| 10 | });
|
---|
| 11 |
|
---|
| 12 | // Initialize the jQuery File Upload plugin
|
---|
| 13 | $('#upload').fileupload({
|
---|
| 14 |
|
---|
| 15 | // This element will accept file drag/drop uploading
|
---|
| 16 | dropZone: $('#drop'),
|
---|
| 17 |
|
---|
| 18 | // This function is called when a file is added to the queue;
|
---|
| 19 | // either via the browse button, or via drag/drop:
|
---|
| 20 | add: function (e, data) {
|
---|
| 21 |
|
---|
| 22 | var tpl = $('<li class="working"><input type="text" value="0" data-width="48" data-height="48"'+
|
---|
| 23 | ' data-fgColor="#0788a5" data-readOnly="1" data-bgColor="#3e4043" /><p></p><span></span></li>');
|
---|
| 24 |
|
---|
| 25 | // Append the file name and file size
|
---|
| 26 | tpl.find('p').text(data.files[0].name)
|
---|
| 27 | .append('<i>' + formatFileSize(data.files[0].size) + '</i>');
|
---|
| 28 |
|
---|
| 29 | // Add the HTML to the UL element
|
---|
| 30 | data.context = tpl.appendTo(ul);
|
---|
| 31 |
|
---|
| 32 | // Initialize the knob plugin
|
---|
| 33 | tpl.find('input').knob();
|
---|
| 34 |
|
---|
| 35 | // Listen for clicks on the cancel icon
|
---|
| 36 | tpl.find('span').click(function(){
|
---|
| 37 |
|
---|
| 38 | if(tpl.hasClass('working')){
|
---|
| 39 | jqXHR.abort();
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | tpl.fadeOut(function(){
|
---|
| 43 | tpl.remove();
|
---|
| 44 | });
|
---|
| 45 |
|
---|
| 46 | });
|
---|
| 47 |
|
---|
| 48 | // Automatically upload the file once it is added to the queue
|
---|
| 49 | var jqXHR = data.submit();
|
---|
| 50 | },
|
---|
| 51 |
|
---|
| 52 | progress: function(e, data){
|
---|
| 53 |
|
---|
| 54 | // Calculate the completion percentage of the upload
|
---|
| 55 | var progress = parseInt(data.loaded / data.total * 100, 10);
|
---|
| 56 |
|
---|
| 57 | // Update the hidden input field and trigger a change
|
---|
| 58 | // so that the jQuery knob plugin knows to update the dial
|
---|
| 59 | data.context.find('input').val(progress).change();
|
---|
| 60 |
|
---|
| 61 | if(progress == 100){
|
---|
| 62 | data.context.removeClass('working');
|
---|
| 63 | }
|
---|
| 64 | },
|
---|
| 65 |
|
---|
| 66 | fail:function(e, data){
|
---|
| 67 | // Something has gone wrong!
|
---|
| 68 | data.context.addClass('error');
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | });
|
---|
| 72 |
|
---|
| 73 |
|
---|
| 74 | // Prevent the default action when a file is dropped on the window
|
---|
| 75 | $(document).on('drop dragover', function (e) {
|
---|
| 76 | e.preventDefault();
|
---|
| 77 | });
|
---|
| 78 |
|
---|
| 79 | // Helper function that formats the file sizes
|
---|
| 80 | function formatFileSize(bytes) {
|
---|
| 81 | if (typeof bytes !== 'number') {
|
---|
| 82 | return '';
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | if (bytes >= 1000000000) {
|
---|
| 86 | return (bytes / 1000000000).toFixed(2) + ' GB';
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | if (bytes >= 1000000) {
|
---|
| 90 | return (bytes / 1000000).toFixed(2) + ' MB';
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | return (bytes / 1000).toFixed(2) + ' KB';
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | }; |
---|