fileupload 插件的使用方法

$(function () {

    $("#uploadAttach").click(function(){

        $("#fileUploadModal").modal('show');

    })

    $("#fileUpload").fileupload({

        url: '/upload',

        autoUpload: true,

        sequentialUploads: true,

        dataType: 'json',

        add: function (e, data) {

            var fileName = data.files[0].name;

            var fileListLength = $('.fileName').find('tr').length;

            for(var i = 0; i < fileListLength; i++) {

                if(fileName === $('.fileName').find('.name').eq(i).text()) {

                    $('.tips').text("不能重复上传!");

                    return false;

                }

            }

            filesList = 'filesList' + fileListLength;

            var filesListHtml = '' + 

                '' + 

                '' + fileName + '

' + 

                '' + 

                '

' + 

                '

' + 

                '' + 

                '' + 

                '';

            $('.fileNames').append(filesListHtml);

            data.context = $('.' + filesList);

            data.submit();

        },

        progress: function (e, data) {

            var progress = parseInt((data.loaded / data.total) * 100, 10);

            data.context.find('.bar').css('width', progress + '%');

        },

        fail: function(e, data){

            data.context.find('.error').text('上传失败!');

        },

        done: function (e, data) {

            $('.fileNames').find('.progress').parent().parent().remove();

            $.each(data.files, function(index, file){

                var res = data.result.split(',');

                if(res[0] === 'success:') {

                    var filesList = "filesList" + $('.fileNames').find('tr').length;

                    var filesListHtml = '' + 

                        '' + 

                        '' + res[3] + '

' + 

                        '' + 

                        '' + 

                        '删除 ' + 

                        '下载' + 

                        '' + 

                        '';

                    $(".fileNames").append(filesListHtml);

                    $("." + filesList).find('.delete').click(function(){

                        window.location.hash = "#top";

                        $.message.confirm("提示", "请注意,删除的附件将无法恢复,是否确认删除?",function(b){

                            if(b) {

                                $.ajax({

                                    async: false,

                                    type: 'post',

                                    url: '/fileUploadHandler/delete',

                                    dataType: 'html',

                                    data: 'id=' + res[2],

                                    success: function(msg){

                                        $('.' + filesList).remove();

                                    }

                                })

                            }

                            window.location.hash = '#bottom';

                        });

                    });

                    $('.' + filesList).find('.download').click(function(){

                        downloadFiles(res[1],res[3]);

                    })

                }

            })

        }

    });

    function downloadFiles(path, fileName){

        window.location.href = "/fileUploadHandler/download?path=" + encodeURIComponent(encodeURIComponent(path)) + "&fileName=" + 

                        encodeURIComponent(encodeURIComponent(fileName));

    }

})

你可能感兴趣的:(fileupload 插件的使用方法)