upload.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /** layui-v2.5.6 MIT License By https://www.layui.com */
  2. ;layui.define("layer", function (e) {
  3. "use strict";
  4. var t = layui.$, i = layui.layer, n = layui.hint(), o = layui.device(), a = {
  5. config: {}, set: function (e) {
  6. var i = this;
  7. return i.config = t.extend({}, i.config, e), i
  8. }, on: function (e, t) {
  9. return layui.onevent.call(this, r, e, t)
  10. }
  11. }, l = function () {
  12. var e = this;
  13. return {
  14. upload: function (t) {
  15. e.upload.call(e, t)
  16. }, reload: function (t) {
  17. e.reload.call(e, t)
  18. }, config: e.config
  19. }
  20. }, r = "upload", u = "layui-upload-file", c = "layui-upload-form", f = "layui-upload-iframe",
  21. s = "layui-upload-choose", p = function (e) {
  22. var i = this;
  23. i.config = t.extend({}, i.config, a.config, e), i.render()
  24. };
  25. p.prototype.config = {
  26. accept: "images",
  27. exts: "",
  28. auto: !0,
  29. bindAction: "",
  30. url: "",
  31. field: "file",
  32. acceptMime: "",
  33. method: "post",
  34. data: {},
  35. drag: !0,
  36. size: 0,
  37. number: 0,
  38. multiple: !1
  39. }, p.prototype.render = function (e) {
  40. var i = this, e = i.config;
  41. e.elem = t(e.elem), e.bindAction = t(e.bindAction), i.file(), i.events()
  42. }, p.prototype.file = function () {
  43. var e = this, i = e.config,
  44. n = e.elemFile = t(['<input class="' + u + '" type="file" accept="' + i.acceptMime + '" name="' + i.field + '"', i.multiple ? " multiple" : "", ">"].join("")),
  45. a = i.elem.next();
  46. (a.hasClass(u) || a.hasClass(c)) && a.remove(), o.ie && o.ie < 10 && i.elem.wrap('<div class="layui-upload-wrap"></div>'), e.isFile() ? (e.elemFile = i.elem, i.field = i.elem[0].name) : i.elem.after(n), o.ie && o.ie < 10 && e.initIE()
  47. }, p.prototype.initIE = function () {
  48. var e = this, i = e.config,
  49. n = t('<iframe id="' + f + '" class="' + f + '" name="' + f + '" frameborder="0"></iframe>'),
  50. o = t(['<form target="' + f + '" class="' + c + '" method="post" key="set-mine" enctype="multipart/form-data" action="' + i.url + '">', "</form>"].join(""));
  51. t("#" + f)[0] || t("body").append(n), i.elem.next().hasClass(c) || (e.elemFile.wrap(o), i.elem.next("." + c).append(function () {
  52. var e = [];
  53. return layui.each(i.data, function (t, i) {
  54. i = "function" == typeof i ? i() : i, e.push('<input type="hidden" name="' + t + '" value="' + i + '">')
  55. }), e.join("")
  56. }()))
  57. }, p.prototype.msg = function (e) {
  58. return i.msg(e, {icon: 2, shift: 6})
  59. }, p.prototype.isFile = function () {
  60. var e = this.config.elem[0];
  61. if (e) return "input" === e.tagName.toLocaleLowerCase() && "file" === e.type
  62. }, p.prototype.preview = function (e) {
  63. var t = this;
  64. window.FileReader && layui.each(t.chooseFiles, function (t, i) {
  65. var n = new FileReader;
  66. n.readAsDataURL(i), n.onload = function () {
  67. e && e(t, i, this.result)
  68. }
  69. })
  70. }, p.prototype.upload = function (e, i) {
  71. var n, a = this, l = a.config, r = a.elemFile[0], u = function () {
  72. var i = 0, n = 0, o = e || a.files || a.chooseFiles || r.files, u = function () {
  73. l.multiple && i + n === a.fileLength && "function" == typeof l.allDone && l.allDone({
  74. total: a.fileLength,
  75. successful: i,
  76. aborted: n
  77. })
  78. };
  79. layui.each(o, function (e, o) {
  80. var r = new FormData;
  81. r.append(l.field, o), layui.each(l.data, function (e, t) {
  82. t = "function" == typeof t ? t() : t, r.append(e, t)
  83. });
  84. var c = {
  85. url: l.url,
  86. type: "post",
  87. data: r,
  88. contentType: !1,
  89. processData: !1,
  90. dataType: "json",
  91. headers: l.headers || {},
  92. success: function (t) {
  93. i++, d(e, t), u()
  94. },
  95. error: function () {
  96. n++, a.msg("请求上传接口出现异常"), m(e), u()
  97. }
  98. };
  99. "function" == typeof l.progress && (c.xhr = function () {
  100. var e = t.ajaxSettings.xhr();
  101. return e.upload.addEventListener("progress", function (e) {
  102. if (e.lengthComputable) {
  103. var t = Math.floor(e.loaded / e.total * 100);
  104. l.progress(t, l.item[0], e)
  105. }
  106. }), e
  107. }), t.ajax(c)
  108. })
  109. }, c = function () {
  110. var e = t("#" + f);
  111. a.elemFile.parent().submit(), clearInterval(p.timer), p.timer = setInterval(function () {
  112. var t, i = e.contents().find("body");
  113. try {
  114. t = i.text()
  115. } catch (n) {
  116. a.msg("获取上传后的响应信息出现异常"), clearInterval(p.timer), m()
  117. }
  118. t && (clearInterval(p.timer), i.html(""), d(0, t))
  119. }, 30)
  120. }, d = function (e, t) {
  121. if (a.elemFile.next("." + s).remove(), r.value = "", "object" != typeof t) try {
  122. t = JSON.parse(t)
  123. } catch (i) {
  124. return t = {}, a.msg("请对上传接口返回有效JSON")
  125. }
  126. "function" == typeof l.done && l.done(t, e || 0, function (e) {
  127. a.upload(e)
  128. })
  129. }, m = function (e) {
  130. l.auto && (r.value = ""), "function" == typeof l.error && l.error(e || 0, function (e) {
  131. a.upload(e)
  132. })
  133. }, h = l.exts, v = function () {
  134. var t = [];
  135. return layui.each(e || a.chooseFiles, function (e, i) {
  136. t.push(i.name)
  137. }), t
  138. }(), g = {
  139. preview: function (e) {
  140. a.preview(e)
  141. }, upload: function (e, t) {
  142. var i = {};
  143. i[e] = t, a.upload(i)
  144. }, pushFile: function () {
  145. return a.files = a.files || {}, layui.each(a.chooseFiles, function (e, t) {
  146. a.files[e] = t
  147. }), a.files
  148. }, resetFile: function (e, t, i) {
  149. var n = new File([t], i);
  150. a.files = a.files || {}, a.files[e] = n
  151. }
  152. }, y = function () {
  153. if ("choose" !== i && !l.auto || (l.choose && l.choose(g), "choose" !== i)) return l.before && l.before(g), o.ie ? o.ie > 9 ? u() : c() : void u()
  154. };
  155. if (v = 0 === v.length ? r.value.match(/[^\/\\]+\..+/g) || [] || "" : v, 0 !== v.length) {
  156. switch (l.accept) {
  157. case"file":
  158. if (h && !RegExp("\\w\\.(" + h + ")$", "i").test(escape(v))) return a.msg("选择的文件中包含不支持的格式"), r.value = "";
  159. break;
  160. case"video":
  161. if (!RegExp("\\w\\.(" + (h || "avi|mp4|wma|rmvb|rm|flash|3gp|flv") + ")$", "i").test(escape(v))) return a.msg("选择的视频中包含不支持的格式"), r.value = "";
  162. break;
  163. case"audio":
  164. if (!RegExp("\\w\\.(" + (h || "mp3|wav|mid") + ")$", "i").test(escape(v))) return a.msg("选择的音频中包含不支持的格式"), r.value = "";
  165. break;
  166. default:
  167. if (layui.each(v, function (e, t) {
  168. RegExp("\\w\\.(" + (h || "jpg|png|gif|bmp|jpeg$") + ")", "i").test(escape(t)) || (n = !0)
  169. }), n) return a.msg("选择的图片中包含不支持的格式"), r.value = ""
  170. }
  171. if (a.fileLength = function () {
  172. var t = 0, i = e || a.files || a.chooseFiles || r.files;
  173. return layui.each(i, function () {
  174. t++
  175. }), t
  176. }(), l.number && a.fileLength > l.number) return a.msg("同时最多只能上传的数量为:" + l.number);
  177. if (l.size > 0 && !(o.ie && o.ie < 10)) {
  178. var F;
  179. if (layui.each(a.chooseFiles, function (e, t) {
  180. if (t.size > 1024 * l.size) {
  181. var i = l.size / 1024;
  182. i = i >= 1 ? i.toFixed(2) + "MB" : l.size + "KB", r.value = "", F = i
  183. }
  184. }), F) return a.msg("文件不能超过" + F)
  185. }
  186. y()
  187. }
  188. }, p.prototype.reload = function (e) {
  189. e = e || {}, delete e.elem, delete e.bindAction;
  190. var i = this, e = i.config = t.extend({}, i.config, a.config, e), n = e.elem.next();
  191. n.attr({name: e.name, accept: e.acceptMime, multiple: e.multiple})
  192. }, p.prototype.events = function () {
  193. var e = this, i = e.config, a = function (t) {
  194. e.chooseFiles = {}, layui.each(t, function (t, i) {
  195. var n = (new Date).getTime();
  196. e.chooseFiles[n + "-" + t] = i
  197. })
  198. }, l = function (t, n) {
  199. var o = e.elemFile,
  200. a = t.length > 1 ? t.length + "个文件" : (t[0] || {}).name || o[0].value.match(/[^\/\\]+\..+/g) || [] || "";
  201. o.next().hasClass(s) && o.next().remove(), e.upload(null, "choose"), e.isFile() || i.choose || o.after('<span class="layui-inline ' + s + '">' + a + "</span>")
  202. };
  203. i.elem.off("upload.start").on("upload.start", function () {
  204. var o = t(this), a = o.attr("lay-data");
  205. if (a) try {
  206. a = new Function("return " + a)(), e.config = t.extend({}, i, a)
  207. } catch (l) {
  208. n.error("Upload element property lay-data configuration item has a syntax error: " + a)
  209. }
  210. e.config.item = o, e.elemFile[0].click()
  211. }), o.ie && o.ie < 10 || i.elem.off("upload.over").on("upload.over", function () {
  212. var e = t(this);
  213. e.attr("lay-over", "")
  214. }).off("upload.leave").on("upload.leave", function () {
  215. var e = t(this);
  216. e.removeAttr("lay-over")
  217. }).off("upload.drop").on("upload.drop", function (n, o) {
  218. var r = t(this), u = o.originalEvent.dataTransfer.files || [];
  219. r.removeAttr("lay-over"), a(u), i.auto ? e.upload(u) : l(u)
  220. }), e.elemFile.off("upload.change").on("upload.change", function () {
  221. var t = this.files || [];
  222. a(t), i.auto ? e.upload() : l(t)
  223. }), i.bindAction.off("upload.action").on("upload.action", function () {
  224. e.upload()
  225. }), i.elem.data("haveEvents") || (e.elemFile.on("change", function () {
  226. t(this).trigger("upload.change")
  227. }), i.elem.on("click", function () {
  228. e.isFile() || t(this).trigger("upload.start")
  229. }), i.drag && i.elem.on("dragover", function (e) {
  230. e.preventDefault(), t(this).trigger("upload.over")
  231. }).on("dragleave", function (e) {
  232. t(this).trigger("upload.leave")
  233. }).on("drop", function (e) {
  234. e.preventDefault(), t(this).trigger("upload.drop", e)
  235. }), i.bindAction.on("click", function () {
  236. t(this).trigger("upload.action")
  237. }), i.elem.data("haveEvents", !0))
  238. }, a.render = function (e) {
  239. var t = new p(e);
  240. return l.call(t)
  241. }, e(r, a)
  242. });