mirror of
https://gitee.com/zoujingli/ThinkAdmin.git
synced 2025-04-06 03:58:04 +08:00
修改文件上传
This commit is contained in:
parent
006750afda
commit
03b142c3ee
@ -1,32 +1,37 @@
|
|||||||
define(['md5'], function (SparkMD5, allowExtsMimes) {
|
define(['md5'], function (SparkMD5) {
|
||||||
allowExtsMimes = JSON.parse('{$exts|raw}');
|
return function (element, UploadedHandler) {
|
||||||
return function (element, InitHandler, UploadedHandler) {
|
var that = this, index;
|
||||||
|
|
||||||
/*! 定义初始化变量 */
|
/*! 定义初始化变量 */
|
||||||
var exts = [], mimes = [], safe, field, uptype, multiple, types, $input, index;
|
this.$ele = $(element);
|
||||||
safe = $(element).attr('data-safe') || '';
|
this.safe = this.$ele.data('safe') || '';
|
||||||
uptype = safe ? 'local' : $(element).attr('data-uptype') || '';
|
this.field = this.$ele.data('field') || 'file';
|
||||||
field = $(element).data('field') || 'file', $input = $('[name="' + field + '"]');
|
this.input = $('[name="_FIELD_"]'.replace('_FIELD_', this.field));
|
||||||
types = ($(element).data('type') || '').split(','), multiple = $(element).attr('data-multiple') > 0;
|
this.uptype = this.safe ? 'local' : this.$ele.attr('data-uptype') || '';
|
||||||
/*! 设置文件选择筛选规则 */
|
|
||||||
for (index in types) if (allowExtsMimes[types[index]]) {
|
/*! 文件的选择筛选 */
|
||||||
mimes.push(allowExtsMimes[types[index]]), exts.push(types[index]);
|
this.types = (this.$ele.data('type') || '').split(',');
|
||||||
|
this.mimes = [], this.exts = [], this.allowExtsMimes = JSON.parse('{$exts|raw}');
|
||||||
|
for (index in this.types) if (this.allowExtsMimes[this.types[index]]) {
|
||||||
|
this.exts.push(this.types[index]), this.mimes.push(this.allowExtsMimes[this.types[index]]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*! 调用初始化组件 */
|
/*! 调用初始化组件 */
|
||||||
renderUploader({exts: exts.join('|'), acceptMime: mimes.join(',')});
|
renderUploader({exts: this.exts.join('|'), acceptMime: this.mimes.join(',')});
|
||||||
|
|
||||||
/*! 初始化上传组件 */
|
/*! 初始化上传组件 */
|
||||||
function renderUploader(options, headers, uploader) {
|
function renderUploader(options, headers, uploader) {
|
||||||
uploader = layui.upload.render({
|
uploader = layui.upload.render({
|
||||||
idx: 0, auto: false, headers: headers || {}, multiple: multiple,
|
idx: 0, auto: false, headers: headers || {}, multiple: that.$ele.attr('data-multiple') > 0,
|
||||||
accept: 'file', elem: element, exts: options.exts, acceptMime: options.acceptMime,
|
accept: 'file', elem: element, exts: options.exts, acceptMime: options.acceptMime,
|
||||||
choose: function (object, files) {
|
choose: function (object, files) {
|
||||||
files = object.pushFile(), $input.data('files', files);
|
files = object.pushFile(), that.input.data('files', files), that.$ele.data('files', files);
|
||||||
for (index in files) md5file(files[index]).then(function (file) {
|
for (index in files) md5file(files[index]).then(function (file) {
|
||||||
$input.data('file', file).data('index', index);
|
that.input.data('file', file).data('index', index), that.$ele.data('file', file).data('index', index);
|
||||||
jQuery.ajax("{:url('@admin/api.upload/state')}", {
|
jQuery.ajax("{:url('@admin/api.upload/state')}", {
|
||||||
data: {xkey: file.xkey, uptype: uptype, safe: safe}, method: 'post', success: function (ret) {
|
data: {xkey: file.xkey, uptype: that.uptype, safe: safe}, method: 'post', success: function (ret) {
|
||||||
file.xurl = ret.data.url;
|
file.xurl = ret.data.url;
|
||||||
if (ret.code === 404) {
|
if (parseInt(ret.code) === 404) {
|
||||||
uploader.config.data.safe = safe;
|
uploader.config.data.safe = safe;
|
||||||
uploader.config.url = ret.data.server;
|
uploader.config.url = ret.data.server;
|
||||||
uploader.config.data.key = ret.data.xkey;
|
uploader.config.data.key = ret.data.xkey;
|
||||||
@ -41,8 +46,8 @@ define(['md5'], function (SparkMD5, allowExtsMimes) {
|
|||||||
uploader.config.data.success_action_status = 200;
|
uploader.config.data.success_action_status = 200;
|
||||||
}
|
}
|
||||||
object.upload(index, file);
|
object.upload(index, file);
|
||||||
} else if (ret.code === 200) {
|
} else if (parseInt(ret.code) === 200) {
|
||||||
UploadedHandler(ret.data.url, file.xkey);
|
UploadedHandler.call(that.$ele, ret.data.url, file);
|
||||||
} else {
|
} else {
|
||||||
$.msg.error(ret.info || ret.error.message || '文件上传出错!');
|
$.msg.error(ret.info || ret.error.message || '文件上传出错!');
|
||||||
}
|
}
|
||||||
@ -58,23 +63,23 @@ define(['md5'], function (SparkMD5, allowExtsMimes) {
|
|||||||
$('[data-upload-progress]').html(n + '%');
|
$('[data-upload-progress]').html(n + '%');
|
||||||
},
|
},
|
||||||
done: function (ret, file) {
|
done: function (ret, file) {
|
||||||
file = $input.data('file');
|
file = that.$ele.data('file');
|
||||||
this.multiple || $.msg.close(this.idx);
|
this.multiple || $.msg.close(this.idx);
|
||||||
if (typeof ret.uploaded === 'undefined' && file.xurl) {
|
if (typeof ret.uploaded === 'undefined' && file.xurl) {
|
||||||
ret = {uploaded: true, url: file.xurl};
|
ret = {uploaded: true, url: file.xurl};
|
||||||
}
|
}
|
||||||
if (ret.uploaded) {
|
if (ret.uploaded) {
|
||||||
if (typeof UploadedHandler === 'function') {
|
if (typeof UploadedHandler === 'function') {
|
||||||
UploadedHandler(ret.url, file);
|
UploadedHandler.call(that.$ele, ret.url, file);
|
||||||
} else {
|
} else {
|
||||||
$input.val(ret.url).trigger('change');
|
that.input.val(ret.url).trigger('change');
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$.msg.error(ret.info || ret.error.message || '文件上传出错!');
|
$.msg.error(ret.info || ret.error.message || '文件上传出错!');
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
allDone: function () {
|
allDone: function () {
|
||||||
$.msg.close(this.idx), $(element).html($(element).data('html'));
|
$.msg.close(this.idx), that.$ele.html(that.$ele.data('html'));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -511,7 +511,7 @@ $(function () {
|
|||||||
var that = this, mode = $(this).attr('data-file') || 'one';
|
var that = this, mode = $(this).attr('data-file') || 'one';
|
||||||
this.attr('data-inited', true).attr('data-multiple', (mode !== 'btn' && mode !== 'one') ? 1 : 0);
|
this.attr('data-inited', true).attr('data-multiple', (mode !== 'btn' && mode !== 'one') ? 1 : 0);
|
||||||
require(['upload'], function (apply) {
|
require(['upload'], function (apply) {
|
||||||
apply(that, null, callback);
|
apply.call(this, that, callback);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user