Ei kuvausta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

upload.js 11KB

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