mirror of
https://gitee.com/zoujingli/ThinkAdmin.git
synced 2025-04-06 03:58:04 +08:00
修改文件上传
This commit is contained in:
parent
03b142c3ee
commit
abf28f42fb
@ -89,6 +89,7 @@ class Upload extends Controller
|
|||||||
$data['signature'] = $token['signature'];
|
$data['signature'] = $token['signature'];
|
||||||
$data['OSSAccessKeyId'] = $token['keyid'];
|
$data['OSSAccessKeyId'] = $token['keyid'];
|
||||||
}
|
}
|
||||||
|
$data['safe'] = intval($this->safe);
|
||||||
$this->success('获取上传参数', $data, 404);
|
$this->success('获取上传参数', $data, 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,45 +1,42 @@
|
|||||||
define(['md5'], function (SparkMD5) {
|
define(['md5'], function (SparkMD5, allowExtsMimes) {
|
||||||
|
allowExtsMimes = JSON.parse('{$exts|raw}');
|
||||||
return function (element, UploadedHandler) {
|
return function (element, UploadedHandler) {
|
||||||
var that = this, index;
|
|
||||||
|
|
||||||
/*! 定义初始化变量 */
|
/*! 定义初始化变量 */
|
||||||
this.$ele = $(element);
|
var opt = {element: $(element)}, index;
|
||||||
this.safe = this.$ele.data('safe') || '';
|
opt.safe = opt.element.data('safe') || '';
|
||||||
this.field = this.$ele.data('field') || 'file';
|
opt.type = opt.element.data('type') || '';
|
||||||
this.input = $('[name="_FIELD_"]'.replace('_FIELD_', this.field));
|
opt.field = opt.element.data('field') || 'file';
|
||||||
this.uptype = this.safe ? 'local' : this.$ele.attr('data-uptype') || '';
|
opt.input = $('[name="_FIELD_"]'.replace('_FIELD_', opt.field));
|
||||||
|
opt.uptype = opt.safe ? 'local' : opt.element.attr('data-uptype') || '';
|
||||||
|
opt.multiple = opt.element.attr('data-multiple') > 0;
|
||||||
/*! 文件的选择筛选 */
|
/*! 文件的选择筛选 */
|
||||||
this.types = (this.$ele.data('type') || '').split(',');
|
this.mimes = [], this.exts = [], this.types = opt.type.split(',');
|
||||||
this.mimes = [], this.exts = [], this.allowExtsMimes = JSON.parse('{$exts|raw}');
|
for (index in this.types) if (allowExtsMimes[this.types[index]]) {
|
||||||
for (index in this.types) if (this.allowExtsMimes[this.types[index]]) {
|
this.exts.push(this.types[index]), this.mimes.push(allowExtsMimes[this.types[index]]);
|
||||||
this.exts.push(this.types[index]), this.mimes.push(this.allowExtsMimes[this.types[index]]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*! 调用初始化组件 */
|
/*! 调用初始化组件 */
|
||||||
renderUploader({exts: this.exts.join('|'), acceptMime: this.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: that.$ele.attr('data-multiple') > 0,
|
idx: 0, auto: false, headers: headers || {}, multiple: opt.multiple,
|
||||||
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(), that.input.data('files', files), that.$ele.data('files', files);
|
files = object.pushFile(), opt.input.data('files', files), opt.element.data('files', files);
|
||||||
for (index in files) md5file(files[index]).then(function (file) {
|
for (index in files) md5file(files[index]).then(function (file) {
|
||||||
that.input.data('file', file).data('index', index), that.$ele.data('file', file).data('index', index);
|
opt.input.data('file', file).data('index', index), opt.element.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: that.uptype, safe: safe}, method: 'post', success: function (ret) {
|
data: {xkey: file.xkey, uptype: opt.uptype, safe: opt.safe}, method: 'post', success: function (ret) {
|
||||||
file.xurl = ret.data.url;
|
file.xurl = ret.data.url;
|
||||||
if (parseInt(ret.code) === 404) {
|
if (parseInt(ret.code) === 404) {
|
||||||
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;
|
||||||
|
uploader.config.data.safe = ret.data.safe;
|
||||||
uploader.config.data.uptype = ret.data.uptype;
|
uploader.config.data.uptype = ret.data.uptype;
|
||||||
if (ret.data.uptype === 'qiniu') {
|
if (ret.data.uptype === 'qiniu') {
|
||||||
uploader.config.data.token = ret.data.token;
|
uploader.config.data.token = ret.data.token;
|
||||||
}
|
} else if (ret.data.uptype === 'alioss') {
|
||||||
if (ret.data.uptype === 'alioss') {
|
|
||||||
uploader.config.data.policy = ret.data.policy;
|
uploader.config.data.policy = ret.data.policy;
|
||||||
uploader.config.data.signature = ret.data.signature;
|
uploader.config.data.signature = ret.data.signature;
|
||||||
uploader.config.data.OSSAccessKeyId = ret.data.OSSAccessKeyId;
|
uploader.config.data.OSSAccessKeyId = ret.data.OSSAccessKeyId;
|
||||||
@ -47,7 +44,7 @@ define(['md5'], function (SparkMD5) {
|
|||||||
}
|
}
|
||||||
object.upload(index, file);
|
object.upload(index, file);
|
||||||
} else if (parseInt(ret.code) === 200) {
|
} else if (parseInt(ret.code) === 200) {
|
||||||
UploadedHandler.call(that.$ele, ret.data.url, file);
|
UploadedHandler.call(opt.element, ret.data.url, file);
|
||||||
} else {
|
} else {
|
||||||
$.msg.error(ret.info || ret.error.message || '文件上传出错!');
|
$.msg.error(ret.info || ret.error.message || '文件上传出错!');
|
||||||
}
|
}
|
||||||
@ -63,23 +60,23 @@ define(['md5'], function (SparkMD5) {
|
|||||||
$('[data-upload-progress]').html(n + '%');
|
$('[data-upload-progress]').html(n + '%');
|
||||||
},
|
},
|
||||||
done: function (ret, file) {
|
done: function (ret, file) {
|
||||||
file = that.$ele.data('file');
|
file = opt.element.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.call(that.$ele, ret.url, file);
|
UploadedHandler.call(opt.element, ret.url, file);
|
||||||
} else {
|
} else {
|
||||||
that.input.val(ret.url).trigger('change');
|
opt.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), that.$ele.html(that.$ele.data('html'));
|
$.msg.close(this.idx), opt.element.html(opt.element.data('html'));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user