require-upload.js
8.1 KB
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
define(['jquery', 'bootstrap', 'backend', 'config', 'plupload'], function ($, undefined, Backend, Config, Plupload) {
var Upload = {
list: {},
config: {
container: document.body,
classname: '.plupload',
},
api: {
//Plupload上传
plupload: function (element, onAfterUpload) {
element = typeof element == 'undefined' ? Upload.config.classname : element;
$(element, Upload.config.container).each(function () {
var id = $(this).prop("id");
var url = $(this).data("url");
var maxsize = $(this).data("maxsize");
var mimetype = $(this).data("mimetype");
var multipart = $(this).data("multipart");
var multiple = $(this).data("multiple");
//上传URL
url = url ? url : Config.upload.uploadurl;
//最大可上传
maxsize = maxsize ? maxsize : Config.upload.maxsize;
//文件类型
mimetype = mimetype ? mimetype : Config.upload.mimetype;
//请求的表单参数
multipart = multipart ? multipart : Config.upload.multipart;
//是否支持批量上传
multiple = multiple ? multiple : Config.upload.multiple;
//生成Plupload实例
Upload.list[id] = new Plupload.Uploader({
runtimes: 'html5,flash,silverlight,html4',
multi_selection: multiple, //是否允许多选批量上传
browse_button: id, // 浏览按钮的ID
container: $(this).parent().get(0), //取按钮的上级元素
flash_swf_url: '/assets/libs/plupload/js/Moxie.swf',
silverlight_xap_url: '/assets/libs/plupload/js/Moxie.xap',
filters: {
max_file_size: maxsize,
mime_types: mimetype
},
url: url,
multipart_params: multipart,
init: {
PostInit: function () {
},
FilesAdded: function (up, files) {
Plupload.each(files, function (file) {
//这里可以改成其它的表现形式
//document.getElementById('filelist').innerHTML += '<div id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b></div>';
});
$("#" + id).data("bakup-html", $("#" + id).html());
//添加后立即上传
setTimeout(function () {
Upload.list[id].start();
}, 1);
},
UploadProgress: function (up, file) {
//这里可以改成其它的表现形式
//document.getElementById(file.id).getElementsByTagName('b')[0].innerHTML = '<span>' + file.percent + "%</span>";
$("#" + id).prop("disabled", true).html("<i class='fa fa-upload'></i> 上传" + file.percent + "%");
},
FileUploaded: function (up, file, info) {
//还原按钮文字及状态
$("#" + id).prop("disabled", false).html($("#" + id).data("bakup-html"));
//这里可以改成其它的表现形式
//document.getElementById(file.id).getElementsByTagName('b')[0].innerHTML += (' [Url]: ' + '<a href="' + url + '" target="_blank">' + url + '</a>');
//这里建议不修改
try {
var ret = JSON.parse(info.response);
if (ret.hasOwnProperty('code')) {
ret.data = ret.code == 200 ? ret.data : ret.data;
ret.code = ret.code == 200 ? 1 : ret.code;
var data = ret.hasOwnProperty("data") && ret.data != "" ? ret.data : null;
var msg = ret.hasOwnProperty("msg") && ret.msg != "" ? ret.msg : "";
$("input[data-plupload-id='" + id + "-text']").val(data);
var afterUpload = $("#" + id).data("after-upload");
if (afterUpload && typeof Upload.api.custom[afterUpload] == 'function') {
Upload.api.custom[afterUpload].call(info, id, data);
}
if (typeof onAfterUpload == 'function') {
onAfterUpload.call(info, id, data);
}
} else {
Toastr.error(e.message + "(code:-2)");
}
} catch (e) {
Toastr.error(e.message + "(code:-1)");
}
},
Error: function (up, err) {
Toastr.error(err.message + "(code:" + err.code + ")");
}
}
});
Upload.list[id].init();
});
},
// AJAX异步上传,主要用于Summernote上传回调
send: function (file, callback) {
var data = new FormData();
data.append("file", file);
$.each(Config.upload.multipart, function (k, v) {
data.append(k, v);
});
$.ajax({
url: Config.upload.uploadurl,
data: data,
cache: false,
contentType: false,
processData: false,
type: 'POST',
dataType: 'json',
success: function (ret) {
if (ret.hasOwnProperty("code")) {
ret.data = ret.code == 200 ? ret : ret.data;
ret.code = ret.code == 200 ? 1 : ret.code;
var data = ret.hasOwnProperty("data") && ret.data != "" ? ret.data : null;
var msg = ret.hasOwnProperty("msg") && ret.msg != "" ? ret.msg : "";
if (ret.code === 1) {
// 如果回调存在,则直接调用回调
if (typeof callback == 'function') {
callback.call(this, data);
} else {
Toastr.success(msg ? msg : __('Operation completed'));
}
} else {
Toastr.error(msg ? msg : __('Operation failed'));
}
} else {
Toastr.error(__('Unknown data format'));
}
}, error: function () {
Toastr.error(__('Network error'));
}
});
},
custom: {
//自定义上传完成回调
afteruploadcallback: function (id, response) {
console.log(this, id, response);
alert("Custom Callback,Response URL:" + response.url);
},
},
}
};
return Upload;
});