demo2.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <title>功能演示二 - 上传组件</title>
  2. <div class="layui-card layadmin-header">
  3. <div class="layui-breadcrumb" lay-filter="breadcrumb">
  4. <a lay-href="">主页</a>
  5. <a><cite>组件</cite></a>
  6. <a><cite>上传组件</cite></a>
  7. <a><cite>功能演示二</cite></a>
  8. </div>
  9. </div>
  10. <div class="layui-fluid">
  11. <div class="layui-row layui-col-space15">
  12. <div class="layui-col-md12">
  13. <div class="layui-card">
  14. <div class="layui-card-header">选完文件后不自动上传</div>
  15. <div class="layui-card-body">
  16. <div class="layui-upload">
  17. <button type="button" class="layui-btn layui-btn-normal" id="test-upload-change">选择文件</button>
  18. <button type="button" class="layui-btn" id="test-upload-change-action">开始上传</button>
  19. </div>
  20. </div>
  21. </div>
  22. </div>
  23. <div class="layui-col-md12">
  24. <div class="layui-card">
  25. <div class="layui-card-header">拖拽上传</div>
  26. <div class="layui-card-body">
  27. <div class="layui-upload-drag" id="test-upload-drag">
  28. <i class="layui-icon"></i>
  29. <p>点击上传,或将文件拖拽到此处</p>
  30. </div>
  31. </div>
  32. </div>
  33. </div>
  34. <div class="layui-col-md12">
  35. <div class="layui-card">
  36. <div class="layui-card-header">高级应用:制作一个多文件列表</div>
  37. <div class="layui-card-body">
  38. <div class="layui-upload">
  39. <button type="button" class="layui-btn layui-btn-normal" id="test-upload-testList">选择多文件</button>
  40. <div class="layui-upload-list">
  41. <table class="layui-table">
  42. <thead>
  43. <tr><th>文件名</th>
  44. <th>大小</th>
  45. <th>状态</th>
  46. <th>操作</th>
  47. </tr></thead>
  48. <tbody id="test-upload-demoList"></tbody>
  49. </table>
  50. </div>
  51. <button type="button" class="layui-btn" id="test-upload-testListAction">开始上传</button>
  52. </div>
  53. </div>
  54. </div>
  55. </div>
  56. <div class="layui-col-md12">
  57. <div class="layui-card">
  58. <div class="layui-card-header">绑定原始文件域</div>
  59. <div class="layui-card-body">
  60. <input type="file" name="file" id="test-upload-primary">
  61. </div>
  62. </div>
  63. </div>
  64. </div>
  65. </div>
  66. <script>
  67. layui.use(['admin', 'upload'], function(){
  68. var $ = layui.jquery
  69. ,upload = layui.upload;
  70. //选完文件后不自动上传
  71. upload.render({
  72. elem: '#test-upload-change'
  73. ,url: '/upload/'
  74. ,auto: false
  75. //,multiple: true
  76. ,bindAction: '#test-upload-change-action'
  77. ,done: function(res){
  78. console.log(res)
  79. }
  80. });
  81. //拖拽上传
  82. upload.render({
  83. elem: '#test-upload-drag'
  84. ,url: '/upload/'
  85. ,done: function(res){
  86. console.log(res)
  87. }
  88. });
  89. //多文件列表示例
  90. var demoListView = $('#test-upload-demoList')
  91. ,uploadListIns = upload.render({
  92. elem: '#test-upload-testList'
  93. ,url: '/upload/'
  94. ,accept: 'file'
  95. ,multiple: true
  96. ,auto: false
  97. ,bindAction: '#test-upload-testListAction'
  98. ,choose: function(obj){
  99. var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
  100. //读取本地文件
  101. obj.preview(function(index, file, result){
  102. var tr = $(['<tr id="upload-'+ index +'">'
  103. ,'<td>'+ file.name +'</td>'
  104. ,'<td>'+ (file.size/1014).toFixed(1) +'kb</td>'
  105. ,'<td>等待上传</td>'
  106. ,'<td>'
  107. ,'<button class="layui-btn layui-btn-mini test-upload-demo-reload layui-hide">重传</button>'
  108. ,'<button class="layui-btn layui-btn-mini layui-btn-danger test-upload-demo-delete">删除</button>'
  109. ,'</td>'
  110. ,'</tr>'].join(''));
  111. //单个重传
  112. tr.find('.test-upload-demo-reload').on('click', function(){
  113. obj.upload(index, file);
  114. });
  115. //删除
  116. tr.find('.test-upload-demo-delete').on('click', function(){
  117. delete files[index]; //删除对应的文件
  118. tr.remove();
  119. uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值,以免删除后出现同名文件不可选
  120. });
  121. demoListView.append(tr);
  122. });
  123. }
  124. ,done: function(res, index, upload){
  125. if(res.code == 0){ //上传成功
  126. var tr = demoListView.find('tr#upload-'+ index)
  127. ,tds = tr.children();
  128. tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
  129. tds.eq(3).html(''); //清空操作
  130. return delete this.files[index]; //删除文件队列已经上传成功的文件
  131. }
  132. this.error(index, upload);
  133. }
  134. ,error: function(index, upload){
  135. var tr = demoListView.find('tr#upload-'+ index)
  136. ,tds = tr.children();
  137. tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
  138. tds.eq(3).find('.test-upload-demo-reload').removeClass('layui-hide'); //显示重传
  139. }
  140. });
  141. //绑定原始文件域
  142. upload.render({
  143. elem: '#test-upload-primary'
  144. ,url: '/upload/'
  145. ,done: function(res){
  146. console.log(res)
  147. }
  148. });
  149. });
  150. </script>