upload-mobile.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*!
  2. @Title: layui.upload 单文件上传 - 全浏览器兼容版
  3. @Author: 贤心
  4. @License:MIT
  5. */
  6. layui.define(['layer-mobile', 'zepto'] , function(exports){
  7. "use strict";
  8. var $ = layui.zepto;
  9. var layer = layui['layer-mobile'];
  10. var device = layui.device();
  11. var elemDragEnter = 'layui-upload-enter';
  12. var elemIframe = 'layui-upload-iframe';
  13. var msgConf = {
  14. icon: 2
  15. ,shift: 6
  16. }, fileType = {
  17. file: '文件'
  18. ,video: '视频'
  19. ,audio: '音频'
  20. };
  21. layer.msg = function(content){
  22. return layer.open({
  23. content: content || ''
  24. ,skin: 'msg'
  25. ,time: 2 //2秒后自动关闭
  26. });
  27. };
  28. var Upload = function(options){
  29. this.options = options;
  30. };
  31. //初始化渲染
  32. Upload.prototype.init = function(){
  33. var that = this, options = that.options;
  34. var body = $('body'), elem = $(options.elem || '.layui-upload-file');
  35. var iframe = $('<iframe id="'+ elemIframe +'" class="'+ elemIframe +'" name="'+ elemIframe +'"></iframe>');
  36. //插入iframe
  37. $('#'+elemIframe)[0] || body.append(iframe);
  38. return elem.each(function(index, item){
  39. item = $(item);
  40. var form = '<form target="'+ elemIframe +'" method="'+ (options.method||'post') +'" key="set-mine" enctype="multipart/form-data" action="'+ (options.url||'') +'"></form>';
  41. var type = item.attr('lay-type') || options.type; //获取文件类型
  42. //包裹ui元素
  43. if(!options.unwrap){
  44. form = '<div class="layui-box layui-upload-button">' + form + '<span class="layui-upload-icon"><i class="layui-icon">&#xe608;</i>'+ (
  45. item.attr('lay-title') || options.title|| ('上传'+ (fileType[type]||'图片') )
  46. ) +'</span></div>';
  47. }
  48. form = $(form);
  49. //拖拽支持
  50. if(!options.unwrap){
  51. form.on('dragover', function(e){
  52. e.preventDefault();
  53. $(this).addClass(elemDragEnter);
  54. }).on('dragleave', function(){
  55. $(this).removeClass(elemDragEnter);
  56. }).on('drop', function(){
  57. $(this).removeClass(elemDragEnter);
  58. });
  59. }
  60. //如果已经实例化,则移除包裹元素
  61. if(item.parent('form').attr('target') === elemIframe){
  62. if(options.unwrap){
  63. item.unwrap();
  64. } else {
  65. item.parent().next().remove();
  66. item.unwrap().unwrap();
  67. }
  68. };
  69. //包裹元素
  70. item.wrap(form);
  71. //触发上传
  72. item.off('change').on('change', function(){
  73. that.action(this, type);
  74. });
  75. });
  76. };
  77. //提交上传
  78. Upload.prototype.action = function(input, type){
  79. var that = this, options = that.options, val = input.value;
  80. var item = $(input), ext = item.attr('lay-ext') || options.ext || ''; //获取支持上传的文件扩展名;
  81. if(!val){
  82. return;
  83. };
  84. //校验文件
  85. switch(type){
  86. case 'file': //一般文件
  87. if(ext && !RegExp('\\w\\.('+ ext +')$', 'i').test(escape(val))){
  88. layer.msg('不支持该文件格式', msgConf);
  89. return input.value = '';
  90. }
  91. break;
  92. case 'video': //视频文件
  93. if(!RegExp('\\w\\.('+ (ext||'avi|mp4|wma|rmvb|rm|flash|3gp|flv') +')$', 'i').test(escape(val))){
  94. layer.msg('不支持该视频格式', msgConf);
  95. return input.value = '';
  96. }
  97. break;
  98. case 'audio': //音频文件
  99. if(!RegExp('\\w\\.('+ (ext||'mp3|wav|mid') +')$', 'i').test(escape(val))){
  100. layer.msg('不支持该音频格式', msgConf);
  101. return input.value = '';
  102. }
  103. break;
  104. default: //图片文件
  105. if(!RegExp('\\w\\.('+ (ext||'jpg|png|gif|bmp|jpeg') +')$', 'i').test(escape(val))){
  106. layer.msg('不支持该图片格式', msgConf);
  107. return input.value = '';
  108. }
  109. break;
  110. }
  111. options.before && options.before(input);
  112. item.parent().submit();
  113. var iframe = $('#'+elemIframe), timer = setInterval(function() {
  114. var res;
  115. try {
  116. res = iframe.contents().find('body').text();
  117. } catch(e) {
  118. layer.msg('上传接口存在跨域', msgConf);
  119. clearInterval(timer);
  120. }
  121. if(res){
  122. clearInterval(timer);
  123. iframe.contents().find('body').html('');
  124. try {
  125. res = JSON.parse(res);
  126. } catch(e){
  127. res = {};
  128. return layer.msg('请对上传接口返回JSON字符', msgConf);
  129. }
  130. typeof options.success === 'function' && options.success(res, input);
  131. }
  132. }, 30);
  133. input.value = '';
  134. };
  135. //暴露接口
  136. exports('upload-mobile', function(options){
  137. var upload = new Upload(options = options || {});
  138. upload.init();
  139. });
  140. });