upload.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /**
  2. @Title: layui.upload 文件上传
  3. @Author: 贤心
  4. @License:MIT
  5. */
  6. layui.define('layer' , function(exports){
  7. "use strict";
  8. var $ = layui.$
  9. ,layer = layui.layer
  10. ,hint = layui.hint()
  11. ,device = layui.device()
  12. //外部接口
  13. ,upload = {
  14. config: {} //全局配置项
  15. //设置全局项
  16. ,set: function(options){
  17. var that = this;
  18. that.config = $.extend({}, that.config, options);
  19. return that;
  20. }
  21. //事件监听
  22. ,on: function(events, callback){
  23. return layui.onevent.call(this, MOD_NAME, events, callback);
  24. }
  25. }
  26. //操作当前实例
  27. ,thisUpload = function(){
  28. var that = this;
  29. return {
  30. upload: function(files){
  31. that.upload.call(that, files);
  32. }
  33. ,config: that.config
  34. }
  35. }
  36. //字符常量
  37. ,MOD_NAME = 'upload', ELEM = '.layui-upload', THIS = 'layui-this', SHOW = 'layui-show', HIDE = 'layui-hide', DISABLED = 'layui-disabled'
  38. ,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'
  39. //构造器
  40. ,Class = function(options){
  41. var that = this;
  42. that.config = $.extend({}, that.config, upload.config, options);
  43. that.render();
  44. };
  45. //默认配置
  46. Class.prototype.config = {
  47. accept: 'images' //允许上传的文件类型:images/file/video/audio
  48. ,exts: '' //允许上传的文件后缀名
  49. ,auto: true //是否选完文件后自动上传
  50. ,bindAction: '' //手动上传触发的元素
  51. ,url: '' //上传地址
  52. ,field: 'file' //文件字段名
  53. ,method: 'post' //请求上传的http类型
  54. ,data: {} //请求上传的额外参数
  55. ,drag: true //是否允许拖拽上传
  56. ,size: 0 //文件限制大小,默认不限制
  57. ,multiple: false //是否允许多文件上传,不支持ie8-9
  58. };
  59. //初始渲染
  60. Class.prototype.render = function(options){
  61. var that = this
  62. ,options = that.config;
  63. options.elem = $(options.elem);
  64. options.bindAction = $(options.bindAction);
  65. that.file();
  66. that.events();
  67. };
  68. //追加文件域
  69. Class.prototype.file = function(){
  70. var that = this
  71. ,options = that.config
  72. ,elemFile = that.elemFile = $([
  73. '<input class="'+ ELEM_FILE +'" type="file" name="'+ options.field +'"'
  74. ,(options.multiple ? ' multiple' : '')
  75. ,'>'
  76. ].join(''))
  77. ,next = options.elem.next();
  78. if(next.hasClass(ELEM_FILE) || next.hasClass(ELEM_FORM)){
  79. next.remove();
  80. }
  81. //包裹ie8/9容器
  82. if(device.ie && device.ie < 10){
  83. options.elem.wrap('<div class="layui-upload-wrap"></div>');
  84. }
  85. that.isFile() ? (
  86. that.elemFile = options.elem
  87. ,options.field = options.elem[0].name
  88. ) : options.elem.after(elemFile);
  89. //初始化ie8/9的Form域
  90. if(device.ie && device.ie < 10){
  91. that.initIE();
  92. }
  93. };
  94. //ie8-9初始化
  95. Class.prototype.initIE = function(){
  96. var that = this
  97. ,options = that.config
  98. ,iframe = $('<iframe id="'+ ELEM_IFRAME +'" class="'+ ELEM_IFRAME +'" name="'+ ELEM_IFRAME +'" frameborder="0"></iframe>')
  99. ,elemForm = $(['<form target="'+ ELEM_IFRAME +'" class="'+ ELEM_FORM +'" method="'+ options.method
  100. ,'" key="set-mine" enctype="multipart/form-data" action="'+ options.url +'">'
  101. ,'</form>'].join(''));
  102. //插入iframe
  103. $('#'+ ELEM_IFRAME)[0] || $('body').append(iframe);
  104. //包裹文件域
  105. if(!options.elem.next().hasClass(ELEM_IFRAME)){
  106. that.elemFile.wrap(elemForm);
  107. //追加额外的参数
  108. options.elem.next('.'+ ELEM_IFRAME).append(function(){
  109. var arr = [];
  110. layui.each(options.data, function(key, value){
  111. arr.push('<input type="hidden" name="'+ key +'" value="'+ value +'">')
  112. });
  113. return arr.join('');
  114. }());
  115. }
  116. };
  117. //异常提示
  118. Class.prototype.msg = function(content){
  119. return layer.msg(content, {
  120. icon: 2
  121. ,shift: 6
  122. });
  123. };
  124. //判断绑定元素是否为文件域本身
  125. Class.prototype.isFile = function(){
  126. var elem = this.config.elem[0];
  127. if(!elem) return;
  128. return elem.tagName.toLocaleLowerCase() === 'input' && elem.type === 'file'
  129. }
  130. //预读图片信息
  131. Class.prototype.preview = function(callback){
  132. var that = this;
  133. if(window.FileReader){
  134. layui.each(that.chooseFiles, function(index, file){
  135. var reader = new FileReader();
  136. reader.readAsDataURL(file);
  137. reader.onload = function(){
  138. callback && callback(index, file, this.result);
  139. }
  140. });
  141. }
  142. };
  143. //执行上传
  144. Class.prototype.upload = function(files, type){
  145. var that = this
  146. ,options = that.config
  147. ,elemFile = that.elemFile[0]
  148. //高级浏览器处理方式,支持跨域
  149. ,ajaxSend = function(){
  150. layui.each(files || that.files || that.chooseFiles || elemFile.files, function(index, file){
  151. var formData = new FormData();
  152. formData.append(options.field, file);
  153. //追加额外的参数
  154. layui.each(options.data, function(key, value){
  155. formData.append(key, value);
  156. });
  157. $.ajax({
  158. url: options.url
  159. ,type: options.method
  160. ,data: formData
  161. ,contentType: false
  162. ,processData: false
  163. ,dataType: 'json'
  164. ,success: function(res){
  165. done(index, res);
  166. }
  167. ,error: function(){
  168. that.msg('请求上传接口出现异常');
  169. error(index);
  170. }
  171. });
  172. });
  173. }
  174. //低版本IE处理方式,不支持跨域
  175. ,iframeSend = function(){
  176. var iframe = $('#'+ ELEM_IFRAME);
  177. that.elemFile.parent().submit();
  178. //获取响应信息
  179. clearInterval(Class.timer);
  180. Class.timer = setInterval(function() {
  181. var res, iframeBody = iframe.contents().find('body');
  182. try {
  183. res = iframeBody.text();
  184. } catch(e) {
  185. that.msg('获取上传后的响应信息出现异常');
  186. clearInterval(Class.timer);
  187. error();
  188. }
  189. if(res){
  190. clearInterval(Class.timer);
  191. iframeBody.html('');
  192. done(0, res);
  193. }
  194. }, 30);
  195. }
  196. //统一回调
  197. ,done = function(index, res){
  198. that.elemFile.next('.'+ ELEM_CHOOSE).remove();
  199. elemFile.value = '';
  200. if(typeof res !== 'object'){
  201. try {
  202. res = JSON.parse(res);
  203. } catch(e){
  204. res = {};
  205. return that.msg('请对上传接口返回有效JSON');
  206. }
  207. }
  208. typeof options.done === 'function' && options.done(res, index || 0, function(files){
  209. that.upload(files);
  210. });
  211. }
  212. //统一网络异常回调
  213. ,error = function(index){
  214. if(options.auto){
  215. elemFile.value = '';
  216. }
  217. typeof options.error === 'function' && options.error(index || 0, function(files){
  218. that.upload(files);
  219. });
  220. }
  221. ,exts = options.exts
  222. ,check ,value = function(){
  223. var arr = [];
  224. layui.each(files || that.chooseFiles, function(i, item){
  225. arr.push(item.name);
  226. });
  227. return arr;
  228. }()
  229. //回调返回的参数
  230. ,args = {
  231. preview: function(callback){
  232. that.preview(callback);
  233. }
  234. ,upload: function(index, file){
  235. var thisFile = {};
  236. thisFile[index] = file;
  237. that.upload(thisFile);
  238. }
  239. ,pushFile: function(){
  240. that.files = that.files || {};
  241. layui.each(that.chooseFiles, function(index, item){
  242. that.files[index] = item;
  243. });
  244. return that.files;
  245. }
  246. }
  247. //提交上传
  248. ,send = function(){
  249. if(type === 'choose'){
  250. return options.choose && options.choose(args);
  251. }
  252. //上传前的回调
  253. options.before && options.before(args);
  254. //IE兼容处理
  255. if(device.ie){
  256. return device.ie > 9 ? ajaxSend() : iframeSend();
  257. }
  258. ajaxSend();
  259. }
  260. //校验文件格式
  261. value = value.length === 0
  262. ? ((elemFile.value.match(/[^\/\\]+\..+/g)||[]) || '')
  263. : value;
  264. switch(options.accept){
  265. case 'file': //一般文件
  266. if(exts && !RegExp('\\w\\.('+ exts +')$', 'i').test(escape(value))){
  267. that.msg('选择的文件中包含不支持的格式');
  268. return elemFile.value = '';
  269. }
  270. break;
  271. case 'video': //视频文件
  272. if(!RegExp('\\w\\.('+ (exts || 'avi|mp4|wma|rmvb|rm|flash|3gp|flv') +')$', 'i').test(escape(value))){
  273. that.msg('选择的视频中包含不支持的格式');
  274. return elemFile.value = '';
  275. }
  276. break;
  277. case 'audio': //音频文件
  278. if(!RegExp('\\w\\.('+ (exts || 'mp3|wav|mid') +')$', 'i').test(escape(value))){
  279. that.msg('选择的音频中包含不支持的格式');
  280. return elemFile.value = '';
  281. }
  282. break;
  283. default: //图片文件
  284. layui.each(value, function(i, item){
  285. if(!RegExp('\\w\\.('+ (exts || 'jpg|png|gif|bmp|jpeg$') +')', 'i').test(escape(item))){
  286. check = true;
  287. }
  288. });
  289. if(check){
  290. that.msg('选择的图片中包含不支持的格式');
  291. return elemFile.value = '';
  292. }
  293. break;
  294. }
  295. //检验文件大小
  296. if(options.size > 0 && !(device.ie && device.ie < 10)){
  297. var limitSize;
  298. layui.each(that.chooseFiles, function(index, file){
  299. if(file.size > 1024*options.size){
  300. var size = options.size/1024;
  301. size = size >= 1
  302. ? (Math.floor(size) + (size%1 > 0 ? size.toFixed(1) : 0)) + 'MB'
  303. : options.size + 'KB'
  304. elemFile.value = '';
  305. limitSize = size;
  306. }
  307. });
  308. if(limitSize) return that.msg('文件不能超过'+ limitSize);
  309. }
  310. send();
  311. };
  312. //事件处理
  313. Class.prototype.events = function(){
  314. var that = this
  315. ,options = that.config
  316. //设置当前选择的文件队列
  317. ,setChooseFile = function(files){
  318. that.chooseFiles = {};
  319. layui.each(files, function(i, item){
  320. var time = new Date().getTime();
  321. that.chooseFiles[time + '-' + i] = item;
  322. });
  323. }
  324. //设置选择的文本
  325. ,setChooseText = function(files, filename){
  326. var elemFile = that.elemFile
  327. ,value = files.length > 1
  328. ? files.length + '个文件'
  329. : ((files[0] || {}).name || (elemFile[0].value.match(/[^\/\\]+\..+/g)||[]) || '');
  330. if(elemFile.next().hasClass(ELEM_CHOOSE)){
  331. elemFile.next().remove();
  332. }
  333. that.upload(null, 'choose');
  334. if(that.isFile() || options.choose) return;
  335. elemFile.after('<span class="layui-inline '+ ELEM_CHOOSE +'">'+ value +'</span>');
  336. };
  337. //点击上传容器
  338. options.elem.off('upload.start').on('upload.start', function(){
  339. var othis = $(this), data = othis.attr('lay-data');
  340. if(data){
  341. try{
  342. data = new Function('return '+ data)();
  343. that.config = $.extend({}, options, data);
  344. } catch(e){
  345. hint.error('Upload element property lay-data configuration item has a syntax error: ' + data)
  346. }
  347. }
  348. that.config.item = othis;
  349. that.elemFile[0].click();
  350. });
  351. //拖拽上传
  352. if(!(device.ie && device.ie < 10)){
  353. options.elem.off('upload.over').on('upload.over', function(){
  354. var othis = $(this)
  355. othis.attr('lay-over', '');
  356. })
  357. .off('upload.leave').on('upload.leave', function(){
  358. var othis = $(this)
  359. othis.removeAttr('lay-over');
  360. })
  361. .off('upload.drop').on('upload.drop', function(e, param){
  362. var othis = $(this), files = param.originalEvent.dataTransfer.files || [];
  363. othis.removeAttr('lay-over');
  364. setChooseFile(files);
  365. if(options.auto){
  366. that.upload(files);
  367. } else {
  368. setChooseText(files);
  369. }
  370. });
  371. }
  372. //文件选择
  373. that.elemFile.off('upload.change').on('upload.change', function(){
  374. var files = this.files || [];
  375. setChooseFile(files);
  376. options.auto ? that.upload() : setChooseText(files); //是否自动触发上传
  377. });
  378. //手动触发上传
  379. options.bindAction.off('upload.action').on('upload.action', function(){
  380. that.upload();
  381. });
  382. //防止事件重复绑定
  383. if(options.elem.data('haveEvents')) return;
  384. that.elemFile.on('change', function(){
  385. $(this).trigger('upload.change');
  386. });
  387. options.elem.on('click', function(){
  388. if(that.isFile()) return;
  389. $(this).trigger('upload.start');
  390. });
  391. if(options.drag){
  392. options.elem.on('dragover', function(e){
  393. e.preventDefault();
  394. $(this).trigger('upload.over');
  395. }).on('dragleave', function(e){
  396. $(this).trigger('upload.leave');
  397. }).on('drop', function(e){
  398. e.preventDefault();
  399. $(this).trigger('upload.drop', e);
  400. });
  401. }
  402. options.bindAction.on('click', function(){
  403. $(this).trigger('upload.action');
  404. });
  405. options.elem.data('haveEvents', true);
  406. };
  407. //核心入口
  408. upload.render = function(options){
  409. var inst = new Class(options);
  410. return thisUpload.call(inst);
  411. };
  412. exports(MOD_NAME, upload);
  413. });