Ignore:
Timestamp:
Oct 20, 2014 2:51:21 PM (11 years ago)
Author:
quyenla
Message:

upload

Location:
pro-bachkim-filespace/sourcecode/assets/js
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • pro-bachkim-filespace/sourcecode/assets/js/manager.js

    r285 r286  
    297297                    uploadStart();
    298298                    upload_init();
     299
    299300                }
    300301
     
    421422                        buttons: {
    422423                            success: {
    423                                 label: "Xong",
    424                                 className: "btn-success",
     424                                label: "Tải lên",
     425                                className: "btn btn-primary",
    425426                                callback: function () {
    426                                    
     427
    427428                                }
    428429                            },
    429                            
    430430                        }
    431431                    };
     
    433433                    return bootbox.dialog(promptOptions);
    434434                }
     435
     436
     437                var upload_init = function () {
     438                    var ul = $('#upload ul');
     439
     440                    $('#drop a').click(function () {
     441                        $(this).parent().find('input').click();
     442                    });
     443                    $('#upload').fileupload({
     444                         dropZone: $('#drop'),
     445                        add: function (e, data) {
     446
     447                            var tpl = $('<li class="working"><input type="text" value="0" data-width="48" data-height="48"' +
     448                                    ' data-fgColor="#0788a5" data-readOnly="1" data-bgColor="#3e4043" /><p></p><span></span></li>');
     449                            tpl.find('p').text(data.files[0].name)
     450                                    .append('<i>' + formatFileSize(data.files[0].size) + '</i>');
     451                            data.context = tpl.appendTo(ul);
     452                            tpl.find('input').knob();
     453                            tpl.find('span').click(function () {
     454
     455                                if (tpl.hasClass('working')) {
     456                                    jqXHR.abort();
     457                                }
     458
     459                                tpl.fadeOut(function () {
     460                                    tpl.remove();
     461                                });
     462
     463                            });
     464                            //alert(JSON.stringify(data, null, 4));
     465                            var jqXHR = data.submit();
     466                        },
     467                        progress: function (e, data) {
     468
     469                            // Calculate the completion percentage of the upload
     470                            var progress = parseInt(data.loaded / data.total * 100, 10);
     471
     472                            // Update the hidden input field and trigger a change
     473                            // so that the jQuery knob plugin knows to update the dial
     474                            data.context.find('input').val(progress).change();
     475
     476                            if (progress == 100) {
     477                                data.context.removeClass('working');
     478                            }
     479                        },
     480                        fail: function (e, data) {
     481                            // Something has gone wrong!
     482                            data.context.addClass('error');
     483                        }
     484
     485                    });
     486                    $(document).on('drop dragover', function (e) {
     487                        e.preventDefault();
     488                    });
     489                    function formatFileSize(bytes) {
     490                        if (typeof bytes !== 'number') {
     491                            return '';
     492                        }
     493
     494                        if (bytes >= 1000000000) {
     495                            return (bytes / 1000000000).toFixed(2) + ' GB';
     496                        }
     497
     498                        if (bytes >= 1000000) {
     499                            return (bytes / 1000000).toFixed(2) + ' MB';
     500                        }
     501
     502                        return (bytes / 1000).toFixed(2) + ' KB';
     503                    }
     504
     505                };
    435506
    436507                var createFolder = function (parent, name) {
  • pro-bachkim-filespace/sourcecode/assets/js/script.js

    r241 r286  
    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 };
Note: See TracChangeset for help on using the changeset viewer.