demo1.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <title>功能演示一 - 上传组件</title>
  2. <style>
  3. .layui-upload-img{width: 92px; height: 92px; margin: 0 10px 10px 0;}
  4. </style>
  5. <div class="layui-card layadmin-header">
  6. <div class="layui-breadcrumb" lay-filter="breadcrumb">
  7. <a lay-href="">主页</a>
  8. <a><cite>组件</cite></a>
  9. <a><cite>上传组件</cite></a>
  10. <a><cite>功能演示一</cite></a>
  11. </div>
  12. </div>
  13. <div class="layui-fluid">
  14. <div class="layui-row layui-col-space15">
  15. <div class="layui-col-md12">
  16. <div class="layui-card">
  17. <div class="layui-card-header">普通图片上传</div>
  18. <div class="layui-card-body">
  19. <div class="layui-upload">
  20. <button type="button" class="layui-btn" id="test-upload-normal">上传图片</button>
  21. <div class="layui-upload-list">
  22. <img class="layui-upload-img" id="test-upload-normal-img">
  23. <p id="test-upload-demoText"></p>
  24. </div>
  25. </div>
  26. <blockquote class="layui-elem-quote">
  27. 为节省服务器开销,以下示例均未配置真实上传接口,所以每次上传都会报提示:请求上传接口出现异常,这属于正常现象。
  28. </blockquote>
  29. </div>
  30. </div>
  31. </div>
  32. <div class="layui-col-md12">
  33. <div class="layui-card">
  34. <div class="layui-card-header">上传多张图片</div>
  35. <div class="layui-card-body">
  36. <div class="layui-upload">
  37. <button type="button" class="layui-btn" id="test-upload-more">多图片上传</button>
  38. <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
  39. 预览图:
  40. <div class="layui-upload-list" id="test-upload-more-list"></div>
  41. </blockquote>
  42. </div>
  43. </div>
  44. </div>
  45. </div>
  46. <div class="layui-col-md12">
  47. <div class="layui-card">
  48. <div class="layui-card-header">指定允许上传的文件类型</div>
  49. <div class="layui-card-body">
  50. <button type="button" class="layui-btn" id="test-upload-type1"><i class="layui-icon"></i>上传文件</button>
  51. <button type="button" class="layui-btn layui-btn-primary" id="test-upload-type2"><i class="layui-icon"></i>只允许压缩文件</button>
  52. <button type="button" class="layui-btn" id="test-upload-type3"><i class="layui-icon"></i>上传视频</button>
  53. <button type="button" class="layui-btn" id="test-upload-type4"><i class="layui-icon"></i>上传音频</button>
  54. </div>
  55. </div>
  56. </div>
  57. <div class="layui-col-md12">
  58. <div class="layui-card">
  59. <div class="layui-card-header">设定文件大小限制</div>
  60. <div class="layui-card-body">
  61. <button type="button" class="layui-btn layui-btn-danger" id="test-upload-size">
  62. <i class="layui-icon"></i>上传图片
  63. </button>
  64. <div class="layui-inline layui-word-aux">
  65. 这里以限制 60KB 为例
  66. </div>
  67. </div>
  68. </div>
  69. </div>
  70. <div class="layui-col-md12">
  71. <div class="layui-card">
  72. <div class="layui-card-header">同时绑定多个元素,并将属性设定在元素上</div>
  73. <div class="layui-card-body">
  74. <button class="layui-btn test-upload-demoMore" lay-data="{url: '/a/'}">上传A</button>
  75. <button class="layui-btn test-upload-demoMore" lay-data="{url: '/b/', size:5}">上传B</button>
  76. <button class="layui-btn test-upload-demoMore" lay-data="{url: '/c/', accept: 'file',size:10}">上传C</button>
  77. </div>
  78. </div>
  79. </div>
  80. </div>
  81. </div>
  82. <script>
  83. layui.use(['admin', 'upload'], function(){
  84. var $ = layui.jquery
  85. ,upload = layui.upload;
  86. //普通图片上传
  87. var uploadInst = upload.render({
  88. elem: '#test-upload-normal'
  89. ,url: '/upload/'
  90. ,before: function(obj){
  91. //预读本地文件示例,不支持ie8
  92. obj.preview(function(index, file, result){
  93. $('#test-upload-normal-img').attr('src', result); //图片链接(base64)
  94. });
  95. }
  96. ,done: function(res){
  97. //如果上传失败
  98. if(res.code > 0){
  99. return layer.msg('上传失败');
  100. }
  101. //上传成功
  102. }
  103. ,error: function(){
  104. //演示失败状态,并实现重传
  105. var demoText = $('#test-upload-demoText');
  106. demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
  107. demoText.find('.demo-reload').on('click', function(){
  108. uploadInst.upload();
  109. });
  110. }
  111. });
  112. //多图片上传
  113. upload.render({
  114. elem: '#test-upload-more'
  115. ,url: '/upload/'
  116. ,multiple: true
  117. ,before: function(obj){
  118. //预读本地文件示例,不支持ie8
  119. obj.preview(function(index, file, result){
  120. $('#test-upload-more-list').append('<img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')
  121. });
  122. }
  123. ,done: function(res){
  124. //上传完毕
  125. }
  126. });
  127. //指定允许上传的文件类型
  128. upload.render({
  129. elem: '#test-upload-type1'
  130. ,url: '/upload/'
  131. ,accept: 'file' //普通文件
  132. ,done: function(res){
  133. console.log(res)
  134. }
  135. });
  136. upload.render({ //允许上传的文件后缀
  137. elem: '#test-upload-type2'
  138. ,url: '/upload/'
  139. ,accept: 'file' //普通文件
  140. ,exts: 'zip|rar|7z' //只允许上传压缩文件
  141. ,done: function(res){
  142. console.log(res)
  143. }
  144. });
  145. upload.render({
  146. elem: '#test-upload-type3'
  147. ,url: '/upload/'
  148. ,accept: 'video' //视频
  149. ,done: function(res){
  150. console.log(res)
  151. }
  152. });
  153. upload.render({
  154. elem: '#test-upload-type4'
  155. ,url: '/upload/'
  156. ,accept: 'audio' //音频
  157. ,done: function(res){
  158. console.log(res)
  159. }
  160. });
  161. //设定文件大小限制
  162. upload.render({
  163. elem: '#test-upload-size'
  164. ,url: '/upload/'
  165. ,size: 60 //限制文件大小,单位 KB
  166. ,done: function(res){
  167. console.log(res)
  168. }
  169. });
  170. //同时绑定多个元素,并将属性设定在元素上
  171. upload.render({
  172. elem: '.test-upload-demoMore'
  173. ,before: function(){
  174. layer.tips('接口地址:'+ this.url, this.item, {tips: 1});
  175. }
  176. ,done: function(res, index, upload){
  177. var item = this.item;
  178. console.log(item); //获取当前触发上传的元素,layui 2.1.0 新增
  179. }
  180. })
  181. });
  182. </script>