修改文件上传

This commit is contained in:
邹景立 2021-06-06 18:58:07 +08:00
parent 814720d56e
commit ed6280c506
3 changed files with 41 additions and 41 deletions

View File

@ -3,14 +3,19 @@ define(['md5'], function (SparkMD5, allowMime) {
return function (element, callable) {
/*! 初始化变量 */
var opt = {element: $(element), exts: [], mimes: [], files: {}, cache: {}, load: 0, count: {total: 0, uploaded: 0}};
opt.element.data('input', opt.element.data('input') || $('input[name="' + (opt.element.data('field') || 'file') + '"]:not([type=file])'));
opt.safe = opt.element.data('safe') ? 1 : 0, opt.hide = opt.element.data('hide-load') ? 1 : 0;
opt.size = opt.element.data('size') || 0, opt.mult = opt.element.data('multiple') > 0;
opt.type = opt.safe ? 'local' : opt.element.attr('data-uptype') || '';
var opt = {elem: $(element), exts: [], mimes: [], files: {}, cache: {}, load: 0, count: {total: 0, uploaded: 0}};
opt.size = opt.elem.data('size') || 0, opt.mult = opt.elem.data('multiple') > 0;
opt.safe = opt.elem.data('safe') ? 1 : 0, opt.hide = opt.elem.data('hide-load') ? 1 : 0;
opt.type = opt.safe ? 'local' : opt.elem.attr('data-uptype') || '';
/*! 查找表单元素, 如果没有找到将不会自动写值 */
if (!opt.elem.data('input') && opt.elem.data('field')) {
var $input = $('input[name="' + opt.elem.data('field') + '"]:not([type=file])');
opt.elem.data('input', $input.size() > 0 ? $input.get(0) : null);
}
/*! 文件选择筛选 */
$((opt.element.data('type') || '').split(',')).map(function (i, ext) {
$((opt.elem.data('type') || '').split(',')).map(function (i, ext) {
if (allowMime[ext]) opt.exts.push(ext), opt.mimes.push(allowMime[ext]);
});
@ -18,15 +23,13 @@ define(['md5'], function (SparkMD5, allowMime) {
opt.uploader = layui.upload.render({
url: '{:sysuri("admin/api.upload/file")}', auto: false, elem: element, accept: 'file', multiple: opt.mult,
exts: opt.exts.join('|'), acceptMime: opt.mimes.join(','), choose: function (object) {
opt.element.triggerHandler('upload.choose', opt.files = object.pushFile());
opt.elem.triggerHandler('upload.choose', opt.files = object.pushFile());
opt.uploader.config.elem.next().val(''), layui.each(opt.files, function (index, file) {
if (opt.size > 0 && file.size > opt.size) {
return delete opt.files[index], $.msg.tips('文件大小超出上传限制!');
}
if (opt.size > 0 && file.size > opt.size) return delete opt.files[index], $.msg.tips('文件大小超出限制!');
opt.load = opt.hide || $.msg.loading('上传进度 <span data-upload-progress>0%</span>');
opt.count.total++, file.index = index, opt.cache[index] = file, delete opt.files[index];
md5file(file).then(function (file) {
opt.element.triggerHandler('upload.hash', file);
opt.elem.triggerHandler('upload.hash', file);
jQuery.ajax("{:url('admin/api.upload/state')}", {
data: {key: file.xkey, uptype: opt.type, safe: opt.safe, name: file.name}, method: 'post', success: function (ret) {
if (parseInt(ret.code) === 404) {
@ -65,9 +68,7 @@ define(['md5'], function (SparkMD5, allowMime) {
});
}, progress: function (number) {
$('[data-upload-progress]').html(number + '%');
opt.element.triggerHandler('upload.progress', {
number: number, event: arguments[2], file: arguments[3]
});
opt.elem.triggerHandler('upload.progress', {number: number, event: arguments[2], file: arguments[3]});
}, done: function (ret, idx) {
/*! 检查单个文件上传返回的结果 */
@ -76,22 +77,22 @@ define(['md5'], function (SparkMD5, allowMime) {
/*! 单个文件上传成功结果处理 */
if (typeof callable === 'function') {
callable.call(opt.element, opt.cache[idx].xurl, opt.cache['id']);
} else {
opt.mult < 1 && $(opt.element.data('input')).val(opt.cache[idx].xurl).trigger('change');
callable.call(opt.elem, opt.cache[idx].xurl, opt.cache['id']);
} else if (opt.mult < 1 && opt.elem.data('input')) {
$(opt.elem.data('input')).val(opt.cache[idx].xurl).trigger('change');
}
(opt.hide || $.msg.close(opt.load)), opt.element.html(opt.element.data('html'));
opt.element.triggerHandler('upload.done', {file: opt.cache[idx], data: ret});
(opt.hide || $.msg.close(opt.load)), opt.elem.html(opt.elem.data('html'));
opt.elem.triggerHandler('upload.done', {file: opt.cache[idx], data: ret});
/*! 所有文件上传完成后结果处理 */
if (++opt.count.uploaded >= opt.count.total) {
if (opt.mult > 0) {
if (opt.mult > 0 && opt.elem.data('input')) {
var urls = [];
for (var i in opt.cache) urls.push(opt.cache[i].xurl)
$(opt.element.data('input')).val(urls.join('|')).trigger('change');
$(opt.elem.data('input')).val(urls.join('|')).trigger('change');
}
opt.element.triggerHandler('upload.complete', {file: opt.cache});
opt.elem.triggerHandler('upload.complete', {file: opt.cache});
(opt.cache = [], opt.files = []), opt.uploader.reload();
}
}

View File

@ -24,7 +24,7 @@
<span class="color-green font-w7 label-required-prev">所属分类</span>
<span class="color-desc margin-left-5">Category Name</span>
<select class="layui-select" lay-search name="cateids">
{foreach $cates as $cate}{if in_array($cate.id, $cate.ids)}
{foreach $cates as $cate}{if in_array($cate.id, $vo.cateids)}
<option selected value="{:arr2str($cate.ids)}">{:join(' ', $cate.names)}</option>
{else}
<option value="{:arr2str($cate.ids)}">{:join(' ', $cate.names)}</option>

View File

@ -538,14 +538,14 @@ $(function () {
/*! 上传单张图片 */
$.fn.uploadOneImage = function () {
return this.each(function ($in, $tpl) {
$in = $(this), $tpl = $('<a data-file="one" class="uploadimage transition"><span class="layui-icon">&#x1006;</span></a>');
$tpl.attr('data-size', $in.data('size') || 0).attr('data-type', $in.data('type') || 'png,jpg,gif');
$tpl.attr('data-field', $in.attr('name') || 'image').data('input', this).find('span').on('click', function (event) {
event.stopPropagation(), $tpl.attr('style', ''), $in.val('');
return this.each(function ($in, $bt) {
$in = $(this), $bt = $('<a data-file="one" class="uploadimage transition"><span class="layui-icon">&#x1006;</span></a>');
$bt.attr('data-size', $in.data('size') || 0).attr('data-file', 'one').attr('data-type', $in.data('type') || 'png,jpg,gif');
$bt.attr('input', $in.get(0)).data('input', this).find('span').on('click', function (event) {
event.stopPropagation(), $bt.attr('style', ''), $in.val('');
});
$in.attr('name', $tpl.attr('data-field')).after($tpl).on('change', function () {
if (this.value) $tpl.css('backgroundImage', 'url(' + encodeURI(this.value) + ')');
$in.attr('name', $bt.attr('data-field')).after($bt).on('change', function () {
if (this.value) $bt.css('backgroundImage', 'url(' + encodeURI(this.value) + ')');
}).trigger('change');
}), this;
};
@ -553,27 +553,26 @@ $(function () {
/*! 上传多张图片 */
$.fn.uploadMultipleImage = function () {
return this.each(function () {
var $button = $('<a class="uploadimage"></a>'), images = this.value ? this.value.split('|') : [];
var $input = $(this), name = $input.attr('name') || 'umt-image', type = $input.data('type') || 'png,jpg,gif';
$button.attr('data-type', type).attr('data-field', name).attr('data-file', 'mut').data('input', this);
$input.attr('name', name).after($button), $button.uploadFile(function (src) {
images.push(src), $input.val(images.join('|')), showImageContainer([src]);
var $in = $(this), $bt = $('<a class="uploadimage"></a>'), imgs = this.value ? this.value.split('|') : [];
$bt.attr('data-size', $in.data('size') || 0).attr('data-file', 'mut').attr('data-type', $in.data('type') || 'png,jpg,gif');
$in.after($bt), $bt.uploadFile(function (src) {
imgs.push(src), $in.val(imgs.join('|')), showImageContainer([src]);
});
if (images.length > 0) showImageContainer(images);
if (imgs.length > 0) showImageContainer(imgs);
function showImageContainer(srcs) {
$(srcs).each(function (idx, src, $image) {
$image = $('<div class="uploadimage uploadimagemtl transition"><a class="layui-icon margin-right-5">&#xe602;</a><a class="layui-icon margin-right-5">&#x1006;</a><a class="layui-icon margin-right-5">&#xe603;</a></div>');
$image.attr('data-tips-image', encodeURI(src)).css('backgroundImage', 'url(' + encodeURI(src) + ')').on('click', 'a', function (event, index, prevs, $item) {
event.stopPropagation(), $item = $(this).parent(), index = $(this).index(), prevs = $button.prevAll('div.uploadimage').length;
event.stopPropagation(), $item = $(this).parent(), index = $(this).index(), prevs = $bt.prevAll('div.uploadimage').length;
if (index === 0 && $item.index() !== prevs) $item.next().after($item);
else if (index === 2 && $item.index() > 1) $item.prev().before($item);
else if (index === 1) $item.remove();
images = [], $button.prevAll('.uploadimage').map(function () {
images.push($(this).attr('data-tips-image'));
imgs = [], $bt.prevAll('.uploadimage').map(function () {
imgs.push($(this).attr('data-tips-image'));
});
images.reverse(), $input.val(images.join('|'));
}), $button.before($image);
imgs.reverse(), $in.val(imgs.join('|'));
}), $bt.before($image);
});
};
}), this;