修正文件上传

This commit is contained in:
邹景立 2020-01-14 21:45:30 +08:00
parent c0c6fd6b26
commit 1cbed30dac
2 changed files with 55 additions and 64 deletions

View File

@ -104,7 +104,7 @@ class Upload extends Controller
} }
list($this->safe, $this->uptype, $this->name) = [boolval(input('safe')), $this->getType(), input('xkey')]; list($this->safe, $this->uptype, $this->name) = [boolval(input('safe')), $this->getType(), input('xkey')];
if (empty($this->name)) $this->name = Storage::name($file->getPathname(), $this->extension, '', 'md5_file'); if (empty($this->name)) $this->name = Storage::name($file->getPathname(), $this->extension, '', 'md5_file');
$info = Storage::instance($this->uptype)->set($this->name, file_get_contents($file->getRealPath()), $this->safe); $info = Storage::instance($this->uptype)->set($this->name, file_get_contents($file->getRealPath()), $this->safe, $file->getOriginalName());
if (is_array($info) && isset($info['url'])) { if (is_array($info) && isset($info['url'])) {
return json(['uploaded' => true, 'filename' => $this->name, 'url' => $this->safe ? $this->name : $info['url']]); return json(['uploaded' => true, 'filename' => $this->name, 'url' => $this->safe ? $this->name : $info['url']]);
} else { } else {

View File

@ -1,86 +1,77 @@
define(['md5'], function (SparkMD5, allowExtsMimes) { define(['md5'], function (SparkMD5, allowExtsMimes) {
allowExtsMimes = JSON.parse('{$exts|raw}'); allowExtsMimes = JSON.parse('{$exts|raw}');
return function (element, UploadedHandler) { return function (element, UploadedHandler, options) {
/*! 定义初始化变量 */ /*! 定义初始化变量 */
var opt = {element: $(element)}, index; options = {element: $(element), exts: [], mimes: [], files: {}, loading: 0};
opt.safe = opt.element.data('safe') || ''; options.safe = options.element.data('safe') || '';
opt.type = opt.element.data('type') || ''; options.type = options.element.data('type') || '';
opt.field = opt.element.data('field') || 'file'; options.types = options.type ? options.type.split(',') : [];
opt.input = $('[name="_field_"]'.replace('_field_', opt.field)); options.field = options.element.data('field') || 'file';
opt.uptype = opt.safe ? 'local' : opt.element.attr('data-uptype') || ''; options.input = $('[name="_field_"]'.replace('_field_', options.field));
opt.multiple = opt.element.attr('data-multiple') > 0; options.uptype = options.safe ? 'local' : options.element.attr('data-uptype') || '';
options.multiple = options.element.attr('data-multiple') > 0;
/*! 文件的选择筛选 */ /*! 文件的选择筛选 */
this.mimes = [], this.exts = [], this.types = opt.type.split(','); for (var index in options.types) if (allowExtsMimes[options.types[index]]) {
for (index in this.types) if (allowExtsMimes[this.types[index]]) { options.exts.push(options.types[index]), options.mimes.push(allowExtsMimes[options.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) { options.uploader = layui.upload.render({
uploader = layui.upload.render({ auto: false, multiple: options.multiple, accept: 'file', elem: element,
idx: 0, auto: false, headers: headers || {}, multiple: opt.multiple, exts: options.exts.join('|'), acceptMime: options.mimes.join(','), choose: function (obj) {
accept: 'file', elem: element, exts: options.exts, acceptMime: options.acceptMime, for (var index in options.files = obj.pushFile()) {
choose: function (object, files) { options.element.data('file', options.files[index]), md5file(options.files[index]).then(function (file) {
files = object.pushFile(), opt.element.data('files', files);
for (index in files) md5file(files[index]).then(function (file) {
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: opt.uptype, safe: opt.safe, name: file.name}, method: 'post', success: function (ret) { data: {xkey: file.xkey, uptype: options.uptype, safe: options.safe, name: file.name}, 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.url = ret.data.server; options.uploader.config.url = ret.data.server;
uploader.config.data.key = ret.data.xkey; options.uploader.config.data.key = ret.data.xkey;
uploader.config.data.safe = ret.data.safe; options.uploader.config.data.safe = ret.data.safe;
uploader.config.data.uptype = ret.data.uptype; options.uploader.config.data.uptype = ret.data.uptype;
if (ret.data.uptype === 'qiniu') { if (ret.data.uptype === 'qiniu') {
uploader.config.data.token = ret.data.token; options.uploader.config.data.token = ret.data.token;
} else if (ret.data.uptype === 'alioss') { } else if (ret.data.uptype === 'alioss') {
uploader.config.data.policy = ret.data.policy; options.uploader.config.data.policy = ret.data.policy;
uploader.config.data.signature = ret.data.signature; options.uploader.config.data.signature = ret.data.signature;
uploader.config.data.OSSAccessKeyId = ret.data.OSSAccessKeyId; options.uploader.config.data.OSSAccessKeyId = ret.data.OSSAccessKeyId;
uploader.config.data.success_action_status = 200; options.uploader.config.data.success_action_status = 200;
uploader.config.data['Content-Disposition'] = 'inline;filename=' + encodeURIComponent(file.name); options.uploader.config.data['Content-Disposition'] = 'inline;filename=' + encodeURIComponent(file.name);
} }
object.upload(index, file); obj.upload(index, file);
} else if (parseInt(ret.code) === 200) { } else if (parseInt(ret.code) === 200) {
UploadedHandler.call(opt.element, file.xurl, file); UploadedHandler.call(options.element, file.xurl, file);
} else { } else {
$.msg.error(ret.info || ret.error.message || '文件上传出错!'); $.msg.error(ret.info || ret.error.message || '文件上传出错!');
} }
} }
}); });
delete files[index];
}); });
}, delete options.files[index];
before: function () {
this.idx = $.msg.loading('上传进度 <span data-upload-progress>0%</span>');
},
progress: function (n) {
$('[data-upload-progress]').html(n + '%');
},
done: function (ret, 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(opt.element, ret.url, file);
} else {
opt.input.val(ret.url).trigger('change');
}
} else {
$.msg.error(ret.info || ret.error.message || '文件上传出错!');
}
},
allDone: function () {
$.msg.close(this.idx), opt.element.html(opt.element.data('html'));
} }
}); }, before: function () {
}; options.loading = $.msg.loading('上传进度 <span data-upload-progress>0%</span>');
}, progress: function (n) {
$('[data-upload-progress]').html(n + '%');
}, done: function (ret) {
this.file = options.element.data('file');
this.multiple || $.msg.close(options.loading);
if (typeof ret.uploaded === 'undefined' && this.file.xurl) {
ret = {uploaded: true, url: this.file.xurl};
}
if (ret.uploaded) {
if (typeof UploadedHandler === 'function') {
UploadedHandler.call(options.element, ret.url, this.file);
} else {
options.input.val(ret.url).trigger('change');
}
} else {
$.msg.error(ret.info || ret.error.message || '文件上传出错!');
}
}, allDone: function () {
$.msg.close(options.loading);
options.element.html(options.element.data('html'));
}
});
}; };
function md5file(file) { function md5file(file) {