/** @Title: layui.upload 文件上传 @Author: 贤心 @License:MIT */ layui.define('layer' , function(exports){ "use strict"; var $ = layui.$ ,layer = layui.layer ,hint = layui.hint() ,device = layui.device() //外部接口 ,upload = { config: {} //全局配置项 //设置全局项 ,set: function(options){ var that = this; that.config = $.extend({}, that.config, options); return that; } //事件监听 ,on: function(events, callback){ return layui.onevent.call(this, MOD_NAME, events, callback); } } //操作当前实例 ,thisUpload = function(){ var that = this; return { upload: function(files){ that.upload.call(that, files); } ,config: that.config } } //字符常量 ,MOD_NAME = 'upload', ELEM = '.layui-upload', THIS = 'layui-this', SHOW = 'layui-show', HIDE = 'layui-hide', DISABLED = 'layui-disabled' ,ELEM_FILE = 'layui-upload-file', ELEM_FORM = 'layui-upload-form', ELEM_IFRAME = 'layui-upload-iframe', ELEM_CHOOSE = 'layui-upload-choose', ELEM_DRAG = 'layui-upload-drag' //构造器 ,Class = function(options){ var that = this; that.config = $.extend({}, that.config, upload.config, options); that.render(); }; //默认配置 Class.prototype.config = { accept: 'images' //允许上传的文件类型:images/file/video/audio ,exts: '' //允许上传的文件后缀名 ,auto: true //是否选完文件后自动上传 ,bindAction: '' //手动上传触发的元素 ,url: '' //上传地址 ,field: 'file' //文件字段名 ,method: 'post' //请求上传的http类型 ,data: {} //请求上传的额外参数 ,drag: true //是否允许拖拽上传 ,size: 0 //文件限制大小,默认不限制 ,number: 0 //允许同时上传的文件数,默认不限制 ,multiple: false //是否允许多文件上传,不支持ie8-9 }; Class.prototype.setData = function(data){ var that = this; that.config.data = data; }; //初始渲染 Class.prototype.render = function(options){ var that = this ,options = that.config; options.elem = $(options.elem); options.bindAction = $(options.bindAction); that.file(); that.events(); }; //追加文件域 Class.prototype.file = function(){ var that = this ,options = that.config ,elemFile = that.elemFile = $([ '' ].join('')) ,next = options.elem.next(); if(next.hasClass(ELEM_FILE) || next.hasClass(ELEM_FORM)){ next.remove(); } //包裹ie8/9容器 if(device.ie && device.ie < 10){ that.elemFiles={}; options.elem.wrap('
'); } that.isFile() ? ( that.elemFile = options.elem ,options.field = options.elem[0].name ) : options.elem.after(elemFile); //初始化ie8/9的Form域 if(device.ie && device.ie < 10){ that.initIE(); } }; //ie8-9初始化 Class.prototype.initIE = function(){ var that = this ,options = that.config ,iframe = $('') ,elemForm = $([''].join('')); //插入iframe $('#'+ ELEM_IFRAME)[0] || $('body').append(iframe); //包裹文件域 if(!options.elem.next().hasClass(ELEM_IFRAME)){ that.elemFile.wrap(elemForm); //追加额外的参数 options.elem.next('.'+ ELEM_IFRAME).append(function(){ var arr = []; layui.each(options.data, function(key, value){ arr.push('') }); //添加token的值 arr.push(''); return arr.join(''); }()); } }; //异常提示 Class.prototype.msg = function(content){ return layer.msg(content, { icon: 2 ,shift: 6 }); }; //判断绑定元素是否为文件域本身 Class.prototype.isFile = function(){ var elem = this.config.elem[0]; if(!elem) return; return elem.tagName.toLocaleLowerCase() === 'input' && elem.type === 'file' } //预读图片信息 Class.prototype.preview = function(callback){ var that = this; if(window.FileReader){ layui.each(that.chooseFiles, function(index, file){ var reader = new FileReader(); reader.readAsDataURL(file); reader.onload = function(){ callback && callback(index, file, this.result); } }); }else{ layui.each(that.chooseFiles, function(index, file){ file.name=file[0]; //file.size=0; callback && callback(index, file, null); }); } }; //执行上传 Class.prototype.upload = function(files, type){ var that = this ,options = that.config ,elemFile = that.elemFile[0]; var successful = 0, aborted = 0 //高级浏览器处理方式,支持跨域 ,ajaxSend = function(){ var items = files || that.files || that.chooseFiles || elemFile.files layui.each(items, function(index, file){ var formData = new FormData(); formData.append(options.field, file); //追加额外的参数 layui.each(options.data, function(key, value){ formData.append(key, value); }); var header = {}; header[TOKEN_KEY] =$webUtil.getToken(); //提交文件 $.ajax({ url: options.url ,type: options.method ,data: formData ,headers:header ,contentType: false ,processData: false ,dataType: 'json' ,success: function(res){ successful++; done(index, res); allDone(); }, // ,xhrFields: { // withCredentials: true // }, // crossDomain: true == !(document.all), error: function(){ aborted++; that.msg('请求上传接口出现异常'); error(index,'请求上传接口出现异常'); allDone(); } }); }); } //低版本IE处理方式,不支持跨域 ,iframeSend = function() { if(that.fileLength<1){ return; } //weidy@2018-12-17.解决IE8和IE9上没有参数的问题 //追加额外的参数 if (options.data) { var arr = []; layui.each(options.data, function (key, value) { arr.push('') }); arr.push(''); } var iframe = $('#' + ELEM_IFRAME); var form = that.elemFile.parent(); //form.html('').append(arr.join('')).append(file).appendTo('body'); var oldEleFile=that.elemFile.clone(true); that.elemFile.remove(); form.append(arr.join('')); var formSubmit=function (index){ $(':file',form).attr('name',function (){ return $(this).attr('id') }) that.elemFile=$(':file',form).eq(index); that.elemFile[0].name=that.config.field; elemFile = that.elemFile[0]; /*if(index>0){ var newIframe=iframe.clone(true); newIframe.attr('id',ELEM_IFRAME+'_'+index).attr('name',ELEM_IFRAME+'_'+index).appendTo(iframe.parent()); form[0].target=ELEM_IFRAME+'_'+index; }*/ form.submit(); //获取响应信息 clearInterval(Class.timer); Class.timer = setInterval(function () { var res = '', iframeBody; try { iframeBody = iframe.contents().find('body'); res = iframeBody.text(); } catch (e) { that.msg('获取上传后的响应信息出现异常'); clearInterval(Class.timer); aborted++; if(aborted+successful