upload.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. var upload_pic = [];
  2. var upload_file = [];
  3. var upload = [];
  4. function loadUpload(i,e,key,url,assets)
  5. {
  6. var value = e.attr('v');
  7. if (typeof(upload[value]) != "undefined") {
  8. //return;
  9. }
  10. upload[value] = true;
  11. upload_pic[value] = [];
  12. upload_file[value] = [];
  13. var id = e.attr('id');
  14. layui.use(['upload','layer'], function() {
  15. var layer = layui.layer;
  16. var layuiUpload = layui.upload;
  17. var uploadInst = layuiUpload.render({
  18. elem: '#' + id
  19. ,data: {'key' : key}
  20. ,field: 'file'
  21. ,url: url
  22. ,multiple: true
  23. ,accept:'file'
  24. ,choose: function(obj){
  25. layer.load();
  26. }
  27. ,done: function(data) {
  28. layer.closeAll('loading');
  29. if (data.status == 1) {
  30. var mul = e.attr('mul');
  31. if (mul == 'yes') {
  32. if (e.attr('t') == 'file') {
  33. if (upload_file[value].length <= 0 && $("#" + value).val()) {
  34. upload_file[value] = $("#" + value).val().split(',');
  35. }
  36. var m = $('.' +value+'_mul');
  37. var i = m.find('li').eq(upload_file[value].length);
  38. if (i.length) {
  39. i.find('a').eq(0).html(data.name);
  40. } else {
  41. m.append('<li><a href="'+data.url+'" target="_blank" title="点此下载" >'+data.name+'</a>&nbsp;&nbsp;<a href="javascript:;" title="点此删除" style="color:red;" onclick="fileDel($(this), \''+value+'\')">[删除]</a></li>');
  42. }
  43. upload_file[value].push(data.url);
  44. $("#" + value).val(upload_file.join(','));
  45. } else {
  46. if (upload_pic[value].length <= 0 && $("#" + value).val()) {
  47. upload_pic[value] = $("#" + value).val().split(',');
  48. }
  49. var m = $('.' +value+'_mul');
  50. var i = m.find('img').eq(upload_pic[value].length);
  51. if (i.length) {
  52. i.attr('src', data.url);
  53. } else {
  54. m.append('<a href="javascript:;" title="点此删除" onclick="picDel($(this), \''+value+'\')"><img src="'+data.url+'" width="150" style="margin-left:5px;" alt="点此删除"/></a>');
  55. }
  56. upload_pic[value].push(data.url);
  57. $("#" + value).val(upload_pic[value].join(','));
  58. }
  59. } else {
  60. if (e.attr('t') == 'file') {
  61. } else {
  62. $('#show_'+value).attr('src',data.url).show();
  63. $('#show_'+value).click(function()
  64. {
  65. picDel($(this), value, 1);
  66. })
  67. }
  68. $('#'+value).val(data.url);
  69. }
  70. } else {
  71. layer.msg(data.msg, {icon: 4});
  72. return false;
  73. }
  74. }
  75. ,error: function(){
  76. //请求异常回调
  77. }
  78. });
  79. });
  80. }
  81. function picDel(e,v,s)
  82. {
  83. if (confirm('确定要删除图片吗?')) {
  84. if (s == 1) {
  85. e.hide();
  86. $("#" + v).val('');
  87. } else {
  88. if (upload_pic[v].length <= 0 && $("#" + v).val()) {
  89. upload_pic[v] = $("#" + v).val().split(',');
  90. }
  91. var p = e.find('img').attr('src');
  92. e.remove();
  93. upload_pic[v].remove(p);
  94. $("#" + v).val(upload_pic[v].join(','));
  95. }
  96. }
  97. }
  98. function fileDel(e,v)
  99. {
  100. if(confirm('确定要删除文件吗?'))
  101. {
  102. if(upload_file[v].length <= 0 && $("#" + v).val())
  103. {
  104. upload_file[v] = $("#" + v).val().split(',');
  105. }
  106. var parent = e.parent();
  107. var p = parent.find('a').eq(0).attr('href');
  108. parent.remove();
  109. upload_file[v].remove(p);
  110. //console.info(pic);
  111. $("#" + v).val(upload_file[v].join(','));
  112. }
  113. }