增加文件上传大小限制

This commit is contained in:
Anyon 2020-09-27 19:11:59 +08:00
parent 70c04ed22d
commit 4100d89ea9
2 changed files with 14 additions and 10 deletions

View File

@ -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('上传进度 <span data-upload-progress>0%</span>');
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 {

View File

@ -532,7 +532,7 @@ $(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-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('');