1. 程式人生 > >layui-圖片上傳,可使用選擇圖片->上傳圖片,預覽圖片,刪除圖片(轉載)

layui-圖片上傳,可使用選擇圖片->上傳圖片,預覽圖片,刪除圖片(轉載)

原文地址:https://gitee.com/AMortal/codes/qt8m6zk30u1g4evr95jhx13

  1. <!DOCTYPE html>

  2. <html>

  3. <head>

  4. <meta charset="utf-8">

  5. <title>layui</title>

  6. <meta name="renderer" content="webkit">

  7. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

  8. <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  9. <link rel="stylesheet" type="text/css" href="common/layui/css/layui.css" media="all">

  10. <style>

  11. .layui-upload .mark_button {

  12. position: absolute;

  13. right: 15px;

  14. }

  15. .upload-img {

  16. position: relative;

  17. display: inline-block;

  18. width: 300px;

  19. height: 200px;

  20. margin: 0 10px 10px 0;

  21. transition: box-shadow .25s;

  22. border-radius: 4px;

  23. box-shadow: 0px 10px 10px -5px rgba(0, 0, 0, 0.5);

  24. transition: 0.25s;

  25. -webkit-transition: 0.25s;

  26. margin-top: 15px;

  27. }

  28. .layui-upload-img {

  29. width: 300px;

  30. height: 200px;

  31. border-radius: 4px;

  32. }

  33. .upload-img:hover {

  34. cursor: pointer;

  35. box-shadow: 0 0 4px rgba(0,0,0,1);

  36. transform: scale(1.2);

  37. -webkit-transform: scale(1.05);

  38. }

  39. .upload-img input {

  40. position: absolute;

  41. left: 0px;

  42. top: 0px;

  43. }

  44. .upload-img button {

  45. position: absolute;

  46. right: 0px;

  47. top: 0px;

  48. border-radius: 6px;

  49. }

  50. </style>

  51. <!-- 注意:如果你直接複製所有程式碼到本地,上述css路徑需要改成你本地的 -->

  52. </head>

  53. <body>

  54. <div class="layui-upload ">

  55. <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">

  56. 預覽圖:

  57. <div class="layui-upload-list" id="imgs">

  58. </div>

  59. </blockquote>

  60. <div class="mark_button">

  61. <button type="button" class="layui-btn layui-btn-normal" id="sele_imgs">選擇檔案</button>

  62. <button type="button" class="layui-btn" id="upload_imgs" disabled>開始上傳</button>

  63. <button type="button" class="layui-btn layui-btn-danger" id="dele_imgs">刪除選中圖片</button>

  64. </div>

  65. </div>

  66. <script type="text/javascript" src="common/layui/layui.all.js"></script>

  67. <script id="img_template" type="text/html">

  68. <div class="upload-img" filename="{{ d.index }}">

  69. <input type="checkbox" name="" lay-skin="primary">

  70. <img src="{{ d.result }}" alt="{{ d.name }}" class="layui-upload-img">

  71. </div>

  72. </script>

  73. <script>

  74. layui.use(['upload', 'laytpl', 'form'], function () {

  75. var $ = layui.jquery

  76. , upload = layui.upload

  77. , laytpl = layui.laytpl

  78. , form = layui.form;

  79. //批量刪除 單擊事件

  80. $('#dele_imgs').click(function () {

  81. $('input:checked').each(function (index, value) {

  82. var filename=$(this).parent().attr("filename");

  83. delete imgFiles[filename];

  84. $(this).parent().remove()

  85. });

  86. });

  87. var imgFiles;

  88. //多圖片上傳

  89. var uploadInst = upload.render({

  90. elem: '#sele_imgs' //開始

  91. , acceptMime: 'image/*'

  92. , url: '/upload/'

  93. , auto: false

  94. , bindAction: '#upload_imgs'

  95. , multiple: true

  96. , size: 1024 * 12

  97. , choose: function (obj) { //選擇圖片後事件

  98. var files = this.files = obj.pushFile(); //將每次選擇的檔案追加到檔案佇列

  99. imgFiles = files;

  100. $('#upload_imgs').prop('disabled',false);

  101. //預讀本地檔案示例,不支援ie8

  102. obj.preview(function (index, file, result) {

  103. var data = {

  104. index: index,

  105. name: file.name,

  106. result: result

  107. };

  108. //將預覽html 追加

  109. laytpl(img_template.innerHTML).render(data, function (html) {

  110. $('#imgs').append(html);

  111. });

  112. //繫結單擊事件

  113. $('#imgs div:last-child>img').click(function () {

  114. var isChecked = $(this).siblings("input").prop("checked");

  115. $(this).siblings("input").prop("checked", !isChecked);

  116. return false;

  117. });

  118. });

  119. }

  120. , before: function (obj) { //上傳前回函式

  121. layer.load(); //上傳loading

  122. }

  123. , done: function (res,index,upload) { //上傳完畢後事件

  124. layer.closeAll('loading'); //關閉loading

  125. //上傳完畢

  126. $('#imgs').html("");//清空操作

  127. top.layer.msg("上傳成功!");

  128. return delete imgFiles[index]; //刪除檔案佇列已經上傳成功的檔案

  129. }

  130. , error: function (index, upload) {

  131. layer.closeAll('loading'); //關閉loading

  132. top.layer.msg("上傳失敗!");

  133. }

  134. });

  135. });

  136. </script>

  137. </body>

  138. </html>

效果圖