jquery.uploadify.js 修改,添加事件前后

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
Uploadify v2.1.0
newsea edited by 2010.2.27
edit the pop mode and add some method such as :
onselected, oncanceled, oninited.
*/
 
if (jQuery) (
    function (jQuery) {
        jQuery.extend(jQuery.fn, {
            uploadify: function (options) {
                jQuery(this).each(function () {
                    settings = jQuery.extend({
                        id: jQuery(this).attr('id'), // The ID of the object being Uploadified
                        uploader: 'uploadify.swf', // The path to the uploadify swf file
                        script: 'uploadify.php', // The path to the uploadify backend upload script
                        expressInstall: null, // The path to the express install swf file
                        folder: '', // The path to the upload folder
                        height: 30, // The height of the flash button
                        width: 110, // The width of the flash button
                        cancelImg: 'cancel.png', // The path to the cancel image for the default file queue item container
                        wmode: 'opaque', // The wmode of the flash file
                        scriptAccess: 'sameDomain', // Set to "always" to allow script access across domains
                        fileDataName: 'Filedata', // The name of the file collection object in the backend upload script
                        method: 'POST', // The method for sending variables to the backend upload script
                        queueSizeLimit: 999, // The maximum size of the file queue
                        simUploadLimit: 1, // The number of simultaneous uploads allowed
                        queueID: false, // The optional ID of the queue container
                        displayData: 'percentage', // Set to "speed" to show the upload speed in the default queue item
                        onInit: function () { }, // Function to run when uploadify is initialized
                        onInited: function () { },
                        onSelect: function () { }, // Function to run when a file is selected
                        onSelected: function () { }, // Function to run when a file is selected
                        onQueueFull: function () { }, // Function to run when the queue reaches capacity
                        onCheck: function () { }, // Function to run when script checks for duplicate files on the server
                        onCancel: function () { }, // Function to run when an item is cleared from the queue
                        onCanceled: function () { },
                        onError: function () { }, // Function to run when an upload item returns an error
                        onProgress: function () { }, // Function to run each time the upload progress is updated
                        onComplete: function () { }, // Function to run when an upload is completed
                        onAllComplete: function () { }  // Functino to run when all uploads are completed
                    }, options);
                    var pagePath = location.pathname;
                    pagePath = pagePath.split('/');
                    pagePath.pop();
                    pagePath = pagePath.join('/') + '/';
                    var data = {};
                    data.uploadifyID = settings.id;
                    data.pagepath = pagePath;
                    if (settings.buttonImg) data.buttonImg = escape(settings.buttonImg);
                    if (settings.buttonText) data.buttonText = escape(settings.buttonText);
                    if (settings.rollover) data.rollover = true;
                    data.script = settings.script;
                    data.folder = escape(settings.folder);
                    if (settings.scriptData) {
                        var scriptDataString = '';
                        for (var name in settings.scriptData) {
                            scriptDataString += '&' + name + '=' + settings.scriptData[name];
                        }
                        data.scriptData = escape(scriptDataString.substr(1));
                    }
                    data.width = settings.width;
                    data.height = settings.height;
                    data.wmode = settings.wmode;
                    data.method = settings.method;
                    data.queueSizeLimit = settings.queueSizeLimit;
                    data.simUploadLimit = settings.simUploadLimit;
                    if (settings.hideButton) data.hideButton = true;
                    if (settings.fileDesc) data.fileDesc = settings.fileDesc;
                    if (settings.fileExt) data.fileExt = settings.fileExt;
                    if (settings.multi) data.multi = true;
                    if (settings.auto) data.auto = true;
                    if (settings.sizeLimit) data.sizeLimit = settings.sizeLimit;
                    if (settings.checkScript) data.checkScript = settings.checkScript;
                    if (settings.fileDataName) data.fileDataName = settings.fileDataName;
                    if (settings.queueID) data.queueID = settings.queueID;
                    if (settings.onInit() !== false) {
                        jQuery(this).css('display', 'none');
                        jQuery(this).after('<div id="' + jQuery(this).attr('id') + 'Uploader"></div>');
                        swfobject.embedSWF(settings.uploader, settings.id + 'Uploader', settings.width, settings.height, '9.0.24', settings.expressInstall, data, { 'quality': 'high', 'wmode': settings.wmode, 'allowScriptAccess': settings.scriptAccess });
                        if (settings.queueID == false) {
                            jQuery("#" + jQuery(this).attr('id') + "Uploader").after('<div id="' + jQuery(this).attr('id') + 'Queue" class="uploadifyQueue"></div>');
                        }
                        settings.onInited();
                    }
                    if (typeof (settings.onOpen) == 'function') {
                        jQuery(this).bind("uploadifyOpen", settings.onOpen);
                    }
                    jQuery(this).bind("uploadifySelect", { 'action': settings.onSelect, 'queueID': settings.queueID }, function (event, ID, fileObj) {
                        if (event.data.action(event, ID, fileObj) !== false) {
                            var byteSize = Math.round(fileObj.size / 1024 * 100) * .01;
                            var suffix = 'KB';
                            if (byteSize > 1000) {
                                byteSize = Math.round(byteSize * .001 * 100) * .01;
                                suffix = 'MB';
                            }
                            var sizeParts = byteSize.toString().split('.');
                            if (sizeParts.length > 1) {
                                byteSize = sizeParts[0] + '.' + sizeParts[1].substr(0, 2);
                            } else {
                                byteSize = sizeParts[0];
                            }
                            if (fileObj.name.length > 20) {
                                fileName = fileObj.name.substr(0, 20) + '...';
                            } else {
                                fileName = fileObj.name;
                            }
                            queue = '#' + jQuery(this).attr('id') + 'Queue';
                            if (event.data.queueID) {
                                queue = '#' + event.data.queueID;
                            }
                            jQuery(queue).append('<div id="' + jQuery(this).attr('id') + ID + '" class="uploadifyQueueItem">\
                                <div class="cancel">\
                                    <a href="javascript:jQuery(\'#' + jQuery(this).attr('id') + '\').uploadifyCancel(\'' + ID + '\')"><img src="' + settings.cancelImg + '" border="0" /></a>\
                                </div>\
                                <span class="fileName">' + fileName + ' (' + byteSize + suffix + ')</span><span class="percentage"></span>\
                                <div class="uploadifyProgress">\
                                    <div id="' + jQuery(this).attr('id') + ID + 'ProgressBar" class="uploadifyProgressBar"><!--Progress Bar--></div>\
                                </div>\
                            </div>');
                            jQuery(queue).show();
 
                            jQuery(queue).css("left", $('#' + jQuery(this).attr('id') + "Uploader").offset().left);
                            settings.onSelected();
                        }
                    });
                    if (typeof (settings.onSelectOnce) == 'function') {
                        jQuery(this).bind("uploadifySelectOnce", settings.onSelectOnce);
                    }
                    jQuery(this).bind("uploadifyQueueFull", { 'action': settings.onQueueFull }, function (event, queueSizeLimit) {
                        if (event.data.action(event, queueSizeLimit) !== false) {
                            alert('The queue is full.  The max size is ' + queueSizeLimit + '.');
                        }
                    });
                    jQuery(this).bind("uploadifyCheckExist", { 'action': settings.onCheck }, function (event, checkScript, fileQueueObj, folder, single) {
                        var postData = new Object();
                        postData = fileQueueObj;
                        postData.folder = pagePath + folder;
                        if (single) {
                            for (var ID in fileQueueObj) {
                                var singleFileID = ID;
                            }
                        }
                        jQuery.post(checkScript, postData, function (data) {
                            for (var key in data) {
                                if (event.data.action(event, checkScript, fileQueueObj, folder, single) !== false) {
                                    var replaceFile = confirm("Do you want to replace the file " + data[key] + "?");
                                    if (!replaceFile) {
                                        document.getElementById(jQuery(event.target).attr('id') + 'Uploader').cancelFileUpload(key, true, true);
                                    }
                                }
                            }
                            if (single) {
                                document.getElementById(jQuery(event.target).attr('id') + 'Uploader').startFileUpload(singleFileID, true);
                            } else {
                                document.getElementById(jQuery(event.target).attr('id') + 'Uploader').startFileUpload(null, true);
                            }
                        }, "json");
                    });
                    jQuery(this).bind("uploadifyCancel", { 'action': settings.onCancel }, function (event, ID, fileObj, data, clearFast) {
                        if (event.data.action(event, ID, fileObj, data, clearFast) !== false) {
                            var fadeSpeed = (clearFast == true) ? 0 : 250;
                            jQuery("#" + jQuery(this).attr('id') + ID).fadeOut(fadeSpeed, function () {
                                if ($(this).siblings().length == 1) {
                                    $(this).parent().hide();
                                }
                                jQuery(this).remove();
                                settings.onCanceled();
                            });
 
                        }
                    });
                    if (typeof (settings.onClearQueue) == 'function') {
                        jQuery(this).bind("uploadifyClearQueue", settings.onClearQueue);
                    }
                    var errorArray = [];
                    jQuery(this).bind("uploadifyError", { 'action': settings.onError }, function (event, ID, fileObj, errorObj) {
                        if (event.data.action(event, ID, fileObj, errorObj) !== false) {
                            var fileArray = new Array(ID, fileObj, errorObj);
                            errorArray.push(fileArray);
                            jQuery("#" + jQuery(this).attr('id') + ID + " .percentage").text(" - " + errorObj.type + " Error");
                            jQuery("#" + jQuery(this).attr('id') + ID).addClass('uploadifyError');
                        }
                    });
                    jQuery(this).bind("uploadifyProgress", { 'action': settings.onProgress, 'toDisplay': settings.displayData }, function (event, ID, fileObj, data) {
                        if (event.data.action(event, ID, fileObj, data) !== false) {
                            jQuery("#" + jQuery(this).attr('id') + ID + "ProgressBar").css('width', data.percentage + '%');
                            if (event.data.toDisplay == 'percentage') displayData = ' - ' + data.percentage + '%';
                            if (event.data.toDisplay == 'speed') displayData = ' - ' + data.speed + 'KB/s';
                            if (event.data.toDisplay == null) displayData = ' ';
                            jQuery("#" + jQuery(this).attr('id') + ID + " .percentage").text(displayData);
                        }
                    });
                    jQuery(this).bind("uploadifyComplete", { 'action': settings.onComplete }, function (event, ID, fileObj, response, data) {
                        if (event.data.action(event, ID, fileObj, unescape(response), data) !== false) {
                            jQuery("#" + jQuery(this).attr('id') + ID + " .percentage").text(' - Completed');
                            jQuery("#" + jQuery(this).attr('id') + ID).fadeOut(250, function () {
                                if ($(this).siblings().length == 1) {
                                    $(this).parent().hide();
                                }
                                jQuery(this).remove();
                            });
                        }
 
                    });
                    if (typeof (settings.onAllComplete) == 'function') {
                        jQuery(this).bind("uploadifyAllComplete", { 'action': settings.onAllComplete }, function (event, uploadObj) {
                            if (event.data.action(event, uploadObj) !== false) {
                                errorArray = [];
                            }
                        });
                    }
                });
            },
            uploadifySettings: function (settingName, settingValue, resetObject) {
                var returnValue = false;
                jQuery(this).each(function () {
                    if (settingName == 'scriptData' && settingValue != null) {
                        if (resetObject) {
                            var scriptData = settingValue;
                        } else {
                            var scriptData = jQuery.extend(settings.scriptData, settingValue);
                        }
                        var scriptDataString = '';
                        for (var name in scriptData) {
                            scriptDataString += '&' + name + '=' + escape(scriptData[name]);
                        }
                        settingValue = scriptDataString.substr(1);
                    }
                    returnValue = document.getElementById(jQuery(this).attr('id') + 'Uploader').updateSettings(settingName, settingValue);
                });
                if (settingValue == null) {
                    if (settingName == 'scriptData') {
                        var returnSplit = unescape(returnValue).split('&');
                        var returnObj = new Object();
                        for (var i = 0; i < returnSplit.length; i++) {
                            var iSplit = returnSplit[i].split('=');
                            returnObj[iSplit[0]] = iSplit[1];
                        }
                        returnValue = returnObj;
                    }
                    return returnValue;
                }
            },
            uploadifyUpload: function (ID) {
                jQuery(this).each(function () {
                    document.getElementById(jQuery(this).attr('id') + 'Uploader').startFileUpload(ID, false);
                });
 
            },
            uploadifyCancel: function (ID) {
                jQuery(this).each(function () {
                    document.getElementById(jQuery(this).attr('id') + 'Uploader').cancelFileUpload(ID, true, false);
                });
            },
            uploadifyClearQueue: function () {
                jQuery(this).each(function () {
                    document.getElementById(jQuery(this).attr('id') + 'Uploader').clearFileUploadQueue(false);
                });
            }
        })
    })(jQuery);
posted @   NewSea  阅读(6137)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
点击右上角即可分享
微信分享提示