upload.js 16 KB

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