0
0

upfile.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <link rel="stylesheet" href="/static/jqueryFileUpload/style.css">
  2. <script src="/static/jqueryFileUpload/jquery.ui.widget.js"></script>
  3. <script src="/static/jqueryFileUpload/jquery.iframe-transport.js"></script>
  4. <script src="/static/jqueryFileUpload/jquery.fileupload.js"></script>
  5. <script src="/static/ddsort/ddsort.min.js"></script>
  6. <div>
  7. <a href="javascript:;" class="btn btn-primary btn-sm jqfivefile" type="button">
  8. <span id="imgUpload_name_{$name}n2">上传文件</span><span id="progress_{$name}n2"></span>
  9. <input id="imgUpload_{$name}n2" name="files" type="file" title="上传文件" accept="{if isset($accept) && $accept}{$accept}{else}*/*{/if}" data-url="{:url('File/upfile')}" />
  10. </a>
  11. {if isset($accept) && $accept}
  12. 只能上传{$accept}类型文件
  13. {/if}
  14. <div id="imgsbox_{$name}n2" class="imgsbox">
  15. <!-- <div><span>dsdfsdfsdf</span> <a href="javascript:;" class="text-danger">删除</a> </div>-->
  16. </div>
  17. </div>
  18. <script>
  19. $(function(){
  20. var imgs = '{$val}';
  21. var multi = parseInt('{$multi}');
  22. var name = "{$name}";
  23. imgs = imgs?imgs.split(','):[];
  24. let initstr = '';
  25. for (let i in imgs){
  26. initstr += '<div>';
  27. if(multi){
  28. initstr += '<input type="hidden" name="'+name+'[]" value="' + imgs[i] + '">';
  29. }else{
  30. initstr += '<input type="hidden" name="'+name+'" value="' + imgs[i] + '">';
  31. }
  32. initstr += '<span>'+format_path(imgs[i])+'</span>&nbsp;&nbsp;';
  33. initstr += '<a href="javascript:;" class="text-danger" onclick="removePic(this)">删除</a>';
  34. initstr += '</div>';
  35. }
  36. if(multi){
  37. $("#imgsbox_{$name}n2").append(initstr);
  38. }else{
  39. $("#imgsbox_{$name}n2").html(initstr);
  40. }
  41. if(multi){
  42. name = name+'[]';
  43. $("#imgUpload_{$name}n2").attr('multiple','multiple');
  44. }else{
  45. $("#imgUpload_{$name}n2").removeAttr('multiple');
  46. }
  47. $("#imgUpload_{$name}n2").fileupload({
  48. dataType: 'json',
  49. done: function (e, data) {
  50. if (data.result && data.result.code == 0) {
  51. var img = '<div>';
  52. img += '<input type="hidden" name="'+name+'" value="' + data.result.data.path + '">';
  53. img += '<span>'+format_path(data.result.data.path)+'</span>&nbsp;&nbsp;';
  54. img += '<a href="javascript:;" class="text-danger" onclick="removePic(this)">删除</a>';
  55. img += '</div>';
  56. if(multi){
  57. $("#imgsbox_{$name}n2").append(img);
  58. }else{
  59. $("#imgsbox_{$name}n2").html(img);
  60. }
  61. } else {
  62. layer.msg(data.result.message);
  63. }
  64. },
  65. change: function (e, data) {
  66. $("#imgUpload_name_{$name}n2").html('上传文件...');
  67. },
  68. progressall: function (e, data) {
  69. var progressnum = parseInt(data.loaded / data.total * 100);
  70. if (progressnum == 100) {
  71. $("#progress_{$name}n2").text('');
  72. $("#imgUpload_name_{$name}n2").html('上传文件');
  73. } else {
  74. $("#progress_{$name}n2").text(progressnum + "%");
  75. }
  76. },
  77. error: function(e,data){
  78. layer.msg('上传请求失败');
  79. }
  80. });
  81. $( '#imgsbox_{$name}n2' ).DDSort({
  82. target: 'div',
  83. floatStyle: {
  84. 'border': '1px solid #ccc',
  85. 'background-color': '#fff'
  86. }
  87. });
  88. });
  89. /**
  90. * 移除图片
  91. */
  92. function removePic(_self){
  93. $(_self).parent().remove();
  94. }
  95. function format_path(path) {
  96. let strs = path.split('/');
  97. return strs[strs.length - 1];
  98. }
  99. </script>