PageSourceSearch

https://www.noblelift.com/res/js/webuploader/uploadRun.js

js noblelift.com collected 2026-10-04 22:48:16 UTC 17,107 bytes, 523 lines download raw bytes

1
2$.fn.UploadRun = function (opt) {
3    //语言版本
4    languageOpt = {
5        cn: {
6            unSupportMsg: '该上传插件不支持您的浏览器!如果您使用的是IE浏览器,请尝试升级 flash 播放器',
7            browseMsg: '点击选择文件',
8            keepAddMsg: '继续添加',
9            allowTypeMsg: '支持格式:',
10            delMsg: '删除',
11            rightMsg: '向右旋转',
12            leftMsg: '向左旋转',
13            overMaxMsg: '文件大小超出',
14            pauseMsg: '上传暂停',
15            failedMsg: '上传失败,请重试',
16            previewMsg: '预览中',
17            notPreviewMsg: '不能预览',
18            selectedMsg: '选中@0个文件,共@1。',
19            successmsg: '已成功上传@0,上传失败@1',
20            countMsg: '共@0张(@1),已上传@2张',
21            failedCountMsg: ',失败@0张',
22            stopMsg: '暂停上传',
23            continueMsg: '继续上传',
24            startMsg: '开始上传',
25            q_TYPE_DENIED: opt.fileExtMsg||"请选择支持的文件格式!",
26            q_EXCEED_NUM_LIMIT: "单次只能上传50个文件!",
27            q_EXCEED_SIZE_LIMIT: "单次上传的文件总量不能超过 @0KB!",
28            q_EXCEED_SIZE: opt.maxSizeMsg ||"单个文件容量过大,最大 @0KB!",
29            q_DUPLICATE: "选择了重复文件!",
30
31        },
32        en: {
33            unSupportMsg: 'WebUploader does not support the browser you are using.',
34            browseMsg: 'Browse',
35            keepAddMsg: 'Keep adding',
36            allowTypeMsg: 'Allow upload format:',
37            delMsg: 'Delete',
38            rightMsg: 'Rotate right',
39            leftMsg: 'Rotate left',
40            overMaxMsg: 'File size exceeds maximum',
41            pauseMsg: 'Pause',
42            failedMsg: 'Upload failed,please try agains',
43            previewMsg: 'Preview',
44            notPreviewMsg: "Can't preview",
45            selectedMsg: 'Selected @0 files,Total @1.',
46            successmsg: 'Success @0,fail@1',
47            countMsg: 'Total @0(@1),Success @',
48            failedCountMsg: ',fail @0',
49            stopMsg: 'Pause',
50            continueMsg: 'Continue',
51            startMsg: 'Start',
52            q_TYPE_DENIED: opt.fileExtMsg ||"Please select a supported file format!",
53            q_EXCEED_NUM_LIMIT: "Only 50 files can be uploaded at a single time!",
54            q_EXCEED_SIZE_LIMIT: "Total file uploads cannot exceed @0KB!",
55            q_EXCEED_SIZE: opt.maxSizeMsg ||"The file is too large,maximum @0KB!",
56            q_DUPLICATE: "Duplicate file selected!",
57
58        }
59    };
60
61    var $ = jQuery,
62
63        $wrap = $(opt.fileId),
64
65        swfUrl = window.location.protocol + '//' + window.location.host + $("script[websiteurl]").attr("websiteurl") + 'Res/webuploader/Uploader.swf',
66
67        uploadUrl = window.location.protocol + '//' + window.location.host + $("script[websiteurl]").attr("websiteurl") + 'Ajax/WhirUpload.aspx?formId=' + opt.formId + '&isPic=' + (opt.isPic ? '1' : '0'),
68
69        // 图片容器
70        $queue = $('<ul class="filelist"></ul>')
71            .appendTo($wrap.find('.queueList')),
72
73        // 状态栏,包括进度和控制按钮
74        $statusBar = $wrap.find('.statusBar'),
75
76        // 文件总体选择信息。
77        $info = $statusBar.find('.info'),
78
79        // 上传按钮
80        $upload = $wrap.find('.uploadBtn'),
81
82        // 没选择文件之前的内容。
83        $placeHolder = $wrap.find('.placeholder'),
84
85        // 总体进度条
86        $progress = $statusBar.find('.progress').hide(),
87
88        // 添加的文件数量
89        fileCount = 0,
90
91        // 添加的文件总大小
92        fileSize = 0,
93
94        //语言版本 cn-中文,en-英文
95        lang = opt.lang || "cn",
96
97        // 优化retina, 在retina下这个值是2
98        ratio = window.devicePixelRatio || 1,
99
100        // 缩略图大小
101        thumbnailWidth = 110 * ratio,
102        thumbnailHeight = 110 * ratio,
103
104        // 可能有pedding, ready, uploading, confirm, done.
105        state = 'pedding',
106
107        // 所有文件的进度信息,key为file id
108        percentages = {},
109
110        supportTransition = (function () {
111            var s = document.createElement('p').style,
112                r = 'transition' in s ||
113                    'WebkitTransition' in s ||
114                    'MozTransition' in s ||
115                    'msTransition' in s ||
116                    'OTransition' in s;
117            s = null;
118            return r;
119        })(),
120
121        // WebUploader实例
122        uploader;
123
124    if (!WebUploader.Uploader.support()) {
125        alert(languageOpt[lang]['unSupportMsg']);
126        throw new Error(languageOpt[lang]['unSupportMsg']);
127    }
128
129    // 实例化
130    uploader = WebUploader.create({
131        pick: {
132            id: opt.filePicker,
133            label: languageOpt[lang]['browseMsg'],
134            multiple: opt.isMulti
135        },
136        dnd: opt.fileId + ' .queueList',
137        paste: document.body,
138        //上传类型
139        accept: {
140            title: 'Images',
141            extensions: opt.fileExt ? opt.fileExt : 'gif,jpg,jpeg,bmp,png',
142            mimeTypes: opt.isPic ? 'image/*' : 'file/*'
143        },
144
145        // swf文件路径
146        swf: swfUrl,
147        //是否允许拖拽
148        disableGlobalDnd: true,
149        //分片上传
150        chunked: true,
151        //上传文件接收地址
152        server: uploadUrl,
153        //最多选择文件数量
154        fileNumLimit: 50,
155        //总文件大小
156        fileSizeLimit: opt.size ? opt.size * 20 * 1024 : 200 * 1024 * 1024,    // 200 M   文件大小限制
157        //单个文件大小                               
158        fileSingleSizeLimit: opt.size ? opt.size * 1024 : 5 * 1024 * 1024    // 5 M  单文件大小限制
159    });
160
161    // 添加“添加文件”的按钮,
162    uploader.addButton({
163        id: opt.filePicker2,
164        label: languageOpt[lang]['keepAddMsg']
165    });
166
167    $placeHolder.append("<div>" + languageOpt[lang]['allowTypeMsg'] + opt.fileExt + "</div>");
168
169
170    // 当有文件添加进来时执行,负责view的创建
171    function addFile(file) {
172        var $li = $('<li id="' + file.id + '">' +
173            '<p class="title">' + file.name + '</p>' +
174            '<p class="imgWrap"></p>' +
175            '<p class="progress"><span></span></p>' +
176            '</li>'),
177
178            $btns = $('<div class="file-panel">' +
179                '<span class="cancel">' + languageOpt[lang]['delMsg'] + '</span>' +
180                '<span class="rotateRight">' + languageOpt[lang]['rightMsg'] + '</span>' +
181                '<span class="rotateLeft">' + languageOpt[lang]['leftMsg'] + '</span></div>').appendTo($li),
182            $prgress = $li.find('p.progress span'),
183            $wrap = $li.find('p.imgWrap'),
184            $info = $('<p class="error"></p>'),
185
186            showError = function (code) {
187                switch (code) {
188                    case 'exceed_size':
189                        text = languageOpt[lang]['overMaxMsg'];
190                        break;
191
192                    case 'interrupt':
193                        text = languageOpt[lang]['pauseMsg'];
194                        break;
195
196                    default:
197                        text = languageOpt[lang]['failedMsg'];
198                        break;
199                }
200
201                $info.text(text).appendTo($li);
202            };
203
204        if (file.getStatus() === 'invalid') {
205            showError(file.statusText);
206        } else {
207            // @todo lazyload
208            $wrap.text(languageOpt[lang]['previewMsg']);
209            uploader.makeThumb(file, function (error, src) {
210                if (error) {
211                    $wrap.text(languageOpt[lang]['notPreviewMsg']);
212                    return;
213                }
214
215                var img = $('<img src="' + src + '">');
216                $wrap.empty().append(img);
217            }, thumbnailWidth, thumbnailHeight);
218
219            percentages[file.id] = [file.size, 0];
220            file.rotation = 0;
221            $upload.removeClass("disabled");
222        }
223
224        file.on('statuschange', function (cur, prev) {
225            if (prev === 'progress') {
226                $prgress.hide().width(0);
227            } else if (prev === 'queued') {
228                $li.off('mouseenter mouseleave');
229                $btns.remove();
230            }
231
232            // 成功
233            if (cur === 'error' || cur === 'invalid') {
234                console.log(file.statusText);
235                showError(file.statusText);
236                percentages[file.id][1] = 1;
237            } else if (cur === 'interrupt') {
238                showError('interrupt');
239            } else if (cur === 'queued') {
240                percentages[file.id][1] = 0;
241            } else if (cur === 'progress') {
242                $info.remove();
243                $prgress.css('display', 'block');
244            } else if (cur === 'complete') {
245                $li.append('<span class="success"></span>');
246            }
247
248            $li.removeClass('state-' + prev).addClass('state-' + cur);
249        });
250
251        $li.on('mouseenter', function () {
252            $btns.stop().animate({ height: 30 });
253        });
254
255        $li.on('mouseleave', function () {
256            $btns.stop().animate({ height: 0 });
257        });
258
259        $btns.on('click', 'span', function () {
260            var index = $(this).index(),
261                deg;
262
263            switch (index) {
264                case 0:
265                    uploader.removeFile(file);
266                    return;
267
268                case 1:
269                    file.rotation += 90;
270                    break;
271
272                case 2:
273                    file.rotation -= 90;
274                    break;
275            }
276
277            if (supportTransition) {
278                deg = 'rotate(' + file.rotation + 'deg)';
279                $wrap.css({
280                    '-webkit-transform': deg,
281                    '-mos-transform': deg,
282                    '-o-transform': deg,
283                    'transform': deg
284                });
285            } else {
286                $wrap.css('filter', 'progid:DXImageTransform.Microsoft.BasicImage(rotation=' + (~~((file.rotation / 90) % 4 + 4) % 4) + ')');
287            }
288        });
289
290        $li.appendTo($queue);
291    }
292
293    // 负责view的销毁
294    function removeFile(file) {
295        var $li = $('#' + file.id);
296
297        delete percentages[file.id];
298        updateTotalProgress();
299        $li.off().find('.file-panel').off().end().remove();
300    }
301
302    function updateTotalProgress() {
303        var loaded = 0,
304            total = 0,
305            spans = $progress.children(),
306            percent;
307
308        $.each(percentages, function (k, v) {
309            total += v[0];
310            loaded += v[0] * v[1];
311        });
312
313        percent = total ? loaded / total : 0;
314
315        spans.eq(0).text(Math.round(percent * 100) + '%');
316        spans.eq(1).css('width', Math.round(percent * 100) + '%');
317        updateStatus();
318    }
319
320    function updateStatus() {
321        var text = '', stats;
322
323        if (state === 'ready') {
324            text = languageOpt[lang]['selectedMsg'].replace("@0", fileCount).replace("@1", WebUploader.formatSize(fileSize));
325        } else if (state === 'confirm') {
326            stats = uploader.getStats();
327            if (stats.uploadFailNum) {
328                text = languageOpt[lang]['successmsg'].replace("@0", stats.successNum).replace("@1", stats.uploadFailNum);
329            }
330
331        } else {
332            stats = uploader.getStats();
333            text = languageOpt[lang]['countMsg'].replace("@0", fileCount).replace("@1", WebUploader.formatSize(fileSize)).replace("@2", stats.successNum);
334
335            if (stats.uploadFailNum) {
336                text = languageOpt[lang]['failedCountMsg'].replace("@0", stats.uploadFailNum);
337            }
338        }
339
340        $info.html(text);
341    }
342
343    function setState(val) {
344        var file, stats;
345
346        if (val === state) {
347            return;
348        }
349
350        $upload.removeClass('state-' + state);
351        $upload.addClass('state-' + val);
352        state = val;
353
354        switch (state) {
355            case 'pedding':
356                $placeHolder.removeClass('element-invisible');
357                $queue.parent().removeClass('filled');
358                $queue.hide();
359                $statusBar.addClass('element-invisible');
360                uploader.refresh();
361                break;
362
363            case 'ready':
364                $placeHolder.addClass('element-invisible');
365                $(opt.filePicker2).removeClass('element-invisible');
366                $queue.parent().addClass('filled');
367                $queue.show();
368                $statusBar.removeClass('element-invisible');
369                uploader.refresh();
370                break;
371
372            case 'uploading':
373                $(opt.filePicker2).addClass('element-invisible');
374                $progress.show();
375                $upload.text(languageOpt[lang]['stopMsg']);
376                break;
377
378            case 'paused':
379                $progress.show();
380                $upload.text(languageOpt[lang]['continueMsg']); startMsg
381                break;
382
383            case 'confirm':
384                $progress.hide();
385                $upload.text(languageOpt[lang]['startMsg']).addClass('disabled');
386
387                stats = uploader.getStats();
388                if (stats.successNum && !stats.uploadFailNum) {
389                    setState('finish');
390                    return;
391                }
392                break;
393            case 'finish':
394                stats = uploader.getStats();
395                if (stats.successNum) {
396                    //alert('上传成功');
397                } else {
398                    // 没有成功的图片,重设
399                    state = 'done';
400                    location.reload();
401                }
402                break;
403        }
404
405        updateStatus();
406    }
407
408    uploader.onUploadProgress = function (file, percentage) {
409        var $li = $('#' + file.id),
410            $percent = $li.find('.progress span');
411
412        $percent.css('width', percentage * 100 + '%');
413        percentages[file.id][1] = percentage;
414        updateTotalProgress();
415    };
416
417    uploader.onFileQueued = function (file) {
418        fileCount++;
419        fileSize += file.size;
420
421        if (fileCount === 1) {
422            $placeHolder.addClass('element-invisible');
423            $statusBar.show();
424        }
425
426        addFile(file);
427        setState('ready');
428        updateTotalProgress();
429    };
430
431    uploader.onFileDequeued = function (file) {
432        fileCount--;
433        fileSize -= file.size;
434
435        if (!fileCount) {
436            setState('pedding');
437        }
438
439        removeFile(file);
440        updateTotalProgress();
441
442    };
443    // 文件上传成功
444    uploader.on('uploadSuccess', function (file, response) {
445        if (response.Result) {
446            // 上传成功
447            if (opt.isMulti)
448                $(opt.fileUrl).val($(opt.fileUrl).val() + "*" + response.Msg);
449            else
450                $(opt.fileUrl).val(response.Msg);
451        } else {
452            // 上传错误或失败
453            var $file = $("#" + file.id);
454            $error = $('<p class="error"></p>');
455            $error.text(response.Msg).appendTo($file);
456            $file.find(".success").remove();
457        }
458    });
459
460    uploader.on('all', function (type) {
461        var stats;
462        switch (type) {
463            case 'uploadFinished':
464                setState('confirm');
465                break;
466            case 'startUpload':
467                setState('uploading');
468                break;
469
470            case 'stopUpload':
471                setState('paused');
472                break;
473        }
474    });
475
476    uploader.onError = function (code) {
477        switch (code) {
478            case 'Q_TYPE_DENIED':
479                alert(languageOpt[lang]['q_TYPE_DENIED']);
480                break;
481            case 'Q_EXCEED_NUM_LIMIT':
482                alert(languageOpt[lang]['q_EXCEED_NUM_LIMIT']);
483                break;
484            case 'Q_EXCEED_SIZE_LIMIT':
485                alert(languageOpt[lang]['q_EXCEED_SIZE_LIMIT'].replace("@0", opt.size ? opt.size * 20 : 200));
486                break;
487            case 'F_EXCEED_SIZE':
488                alert(languageOpt[lang]['q_EXCEED_SIZE'].replace("@0", opt.size ? opt.size : 5));
489                break;
490            case 'F_DUPLICATE':
491                alert(languageOpt[lang]['q_DUPLICATE']);
492                break;
493        }
494    };
495
496    $upload.on('click', function () {
497        if ($(this).hasClass('disabled')) {
498            return false;
499        }
500
501        if (state === 'ready') {
502            uploader.upload();
503        } else if (state === 'paused') {
504            uploader.upload();
505        } else if (state === 'uploading') {
506            uploader.stop();
507        }
508    });
509
510    $info.on('click', '.retry', function () {
511        uploader.retry();
512    });
513
514    $info.on('click', '.ignore', function () {
515        alert('todo');
516    });
517
518    $upload.addClass('state-' + state);
519
520    updateTotalProgress();
521
522}
523

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.