diff --git a/app/admin/view/api/upload.js b/app/admin/view/api/upload.js index 23f420a98..e6de1c9a0 100644 --- a/app/admin/view/api/upload.js +++ b/app/admin/view/api/upload.js @@ -2,10 +2,11 @@ define(['md5'], function (SparkMD5, allowMime) { allowMime = JSON.parse('{$exts|raw}'); return function (element, callable, option) { /*! 初始化变量 */ - option = {element: $(element), exts: [], mimes: [], files: {}, cache: {}, load: 0, count: {total: 0, uploaded: 0}}; - option.safe = option.element.data('safe') ? 1 : 0, option.field = option.element.data('field') || 'file'; - option.hload = option.element.data('hide-load') ? 1 : 0, option.input = $('[name="_field_"]'.replace('_field_', option.field)); - option.uptype = option.safe ? 'local' : option.element.attr('data-uptype') || '', option.multiple = option.element.attr('data-multiple') > 0; + option = {element: $(element), exts: [], mimes: [], files: {}, cache: {}, load: 0}; + option.count = {total: 0, uploaded: 0}, option.size = option.element.data('size') || 0; + option.safe = option.element.data('safe') ? 1 : 0, option.hload = option.element.data('hide-load') ? 1 : 0; + option.field = option.element.data('field') || 'file', option.input = $('[name="_field_"]'.replace('_field_', option.field)); + option.uptype = option.safe ? 'local' : option.element.attr('data-uptype') || '', option.multiple = option.element.data('multiple') > 0; /*! 文件选择筛选 */ $((option.element.data('type') || '').split(',')).map(function (i, ext) { if (allowMime[ext]) option.exts.push(ext), option.mimes.push(allowMime[ext]); @@ -13,11 +14,14 @@ define(['md5'], function (SparkMD5, allowMime) { /*! 初始化上传组件 */ option.uploader = layui.upload.render({ auto: false, elem: element, accept: 'file', multiple: option.multiple, - exts: option.exts.join('|'), acceptMime: option.mimes.join(','), choose: function (obj) { - for (var i in (option.files = obj.pushFile())) { + exts: option.exts.join('|'), acceptMime: option.mimes.join(','), choose: function (object, index) { + for (index in (option.files = object.pushFile())) { + if (option.size > 0 && option.files[index].size > option.size) { + return delete option.files[index], $.msg.tips('文件大小超出上传限制!'); + } option.load = option.hload || $.msg.loading('上传进度 0%'); - option.count.total++, option.files[i].index = i, option.cache[i] = option.files[i], delete option.files[i]; - md5file(option.cache[i]).then(function (file) { + option.count.total++, option.files[index].index = index, option.cache[index] = option.files[index], delete option.files[index]; + md5file(option.cache[index]).then(function (file) { option.element.triggerHandler('upload.hash', file); jQuery.ajax("{:url('admin/api.upload/state')}", { data: {xkey: file.xkey, uptype: option.uptype, safe: option.safe, name: file.name}, method: 'post', success: function (ret) { @@ -36,7 +40,7 @@ define(['md5'], function (SparkMD5, allowMime) { option.uploader.config.data.success_action_status = 200; option.uploader.config.data['Content-Disposition'] = 'inline;filename=' + encodeURIComponent(file.name); } - obj.upload(file.index, file); + object.upload(file.index, file); } else if (parseInt(ret.code) === 200) { option.uploader.config.done({uploaded: true, url: file.xurl}, file.index); } else { diff --git a/public/static/admin.js b/public/static/admin.js index 3ebdc624c..0447a1b20 100644 --- a/public/static/admin.js +++ b/public/static/admin.js @@ -532,7 +532,7 @@ $(function () { $.fn.uploadOneImage = function () { return this.each(function ($in, $tpl) { $in = $(this), $tpl = $(''); - $tpl.attr('data-type', $in.data('type') || 'png,jpg,gif'); + $tpl.attr('data-type', $in.data('type') || 'png,jpg,gif').attr('data-size', $in.data('size') || 0); $tpl.attr('data-field', $in.attr('name') || 'image').data('input', this); $tpl.find('span').on('click', function (event) { event.stopPropagation(), $tpl.attr('style', ''), $in.val('');