修改文件上传

This commit is contained in:
Anyon 2020-01-02 16:13:29 +08:00
parent 03b142c3ee
commit abf28f42fb
2 changed files with 24 additions and 26 deletions

View File

@ -89,6 +89,7 @@ class Upload extends Controller
$data['signature'] = $token['signature'];
$data['OSSAccessKeyId'] = $token['keyid'];
}
$data['safe'] = intval($this->safe);
$this->success('获取上传参数', $data, 404);
}

View File

@ -1,45 +1,42 @@
define(['md5'], function (SparkMD5) {
define(['md5'], function (SparkMD5, allowExtsMimes) {
allowExtsMimes = JSON.parse('{$exts|raw}');
return function (element, UploadedHandler) {
var that = this, index;
/*! 定义初始化变量 */
this.$ele = $(element);
this.safe = this.$ele.data('safe') || '';
this.field = this.$ele.data('field') || 'file';
this.input = $('[name="_FIELD_"]'.replace('_FIELD_', this.field));
this.uptype = this.safe ? 'local' : this.$ele.attr('data-uptype') || '';
var opt = {element: $(element)}, index;
opt.safe = opt.element.data('safe') || '';
opt.type = opt.element.data('type') || '';
opt.field = opt.element.data('field') || 'file';
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.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]]);
this.mimes = [], this.exts = [], this.types = opt.type.split(',');
for (index in this.types) if (allowExtsMimes[this.types[index]]) {
this.exts.push(this.types[index]), this.mimes.push(allowExtsMimes[this.types[index]]);
}
/*! 调用初始化组件 */
renderUploader({exts: this.exts.join('|'), acceptMime: this.mimes.join(',')});
/*! 初始化上传组件 */
function renderUploader(options, headers, uploader) {
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,
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) {
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')}", {
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;
if (parseInt(ret.code) === 404) {
uploader.config.data.safe = safe;
uploader.config.url = ret.data.server;
uploader.config.data.key = ret.data.xkey;
uploader.config.data.safe = ret.data.safe;
uploader.config.data.uptype = ret.data.uptype;
if (ret.data.uptype === 'qiniu') {
uploader.config.data.token = ret.data.token;
}
if (ret.data.uptype === 'alioss') {
} else if (ret.data.uptype === 'alioss') {
uploader.config.data.policy = ret.data.policy;
uploader.config.data.signature = ret.data.signature;
uploader.config.data.OSSAccessKeyId = ret.data.OSSAccessKeyId;
@ -47,7 +44,7 @@ define(['md5'], function (SparkMD5) {
}
object.upload(index, file);
} else if (parseInt(ret.code) === 200) {
UploadedHandler.call(that.$ele, ret.data.url, file);
UploadedHandler.call(opt.element, ret.data.url, file);
} else {
$.msg.error(ret.info || ret.error.message || '文件上传出错!');
}
@ -63,23 +60,23 @@ define(['md5'], function (SparkMD5) {
$('[data-upload-progress]').html(n + '%');
},
done: function (ret, file) {
file = that.$ele.data('file');
file = opt.element.data('file');
this.multiple || $.msg.close(this.idx);
if (typeof ret.uploaded === 'undefined' && file.xurl) {
ret = {uploaded: true, url: file.xurl};
}
if (ret.uploaded) {
if (typeof UploadedHandler === 'function') {
UploadedHandler.call(that.$ele, ret.url, file);
UploadedHandler.call(opt.element, ret.url, file);
} else {
that.input.val(ret.url).trigger('change');
opt.input.val(ret.url).trigger('change');
}
} else {
$.msg.error(ret.info || ret.error.message || '文件上传出错!');
}
},
allDone: function () {
$.msg.close(this.idx), that.$ele.html(that.$ele.data('html'));
$.msg.close(this.idx), opt.element.html(opt.element.data('html'));
}
});
};