upload.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. var upload_pic = [];
  2. var upload_file = [];
  3. var upload = [];
  4. function loadUpload(i,e,key,upload,assets)
  5. {
  6. var value = e.attr('v');
  7. if (typeof(upload[value]) != "undefined") {
  8. return;
  9. }
  10. upload[value] = true;
  11. var id = e.attr('id');
  12. layui.use(['upload','layer'], function() {
  13. var layer = layui.layer;
  14. var layer_1 = false;
  15. var layuiUpload = layui.upload;
  16. var uploadInst = layuiUpload.render({
  17. elem: '#' + id
  18. ,data: {'key' : key}
  19. ,field: 'file'
  20. ,url: upload
  21. ,multiple: true
  22. ,accept:'file'
  23. ,choose: function(obj){
  24. layer_1 = layer.load(0, {shade: false});
  25. }
  26. ,done: function(data) {
  27. //var data = eval('('+res+')');
  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.length <= 0 && $("#" + value).val()) {
  34. upload_file = $("#" + value).val().split(',');
  35. }
  36. var m = $('.' +value+'_mul');
  37. var i = m.find('li').eq(upload_file.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.push(data.url);
  44. $("#" + value).val(upload_file.join(','));
  45. } else {
  46. if (upload_pic.length <= 0 && $("#" + value).val()) {
  47. upload_pic = $("#" + value).val().split(',');
  48. }
  49. var m = $('.' +value+'_mul');
  50. var i = m.find('img').eq(upload_pic.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.push(data.url);
  57. $("#" + value).val(upload_pic.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. //$("#"+file.id).find('.data').html('上传完毕');
  69. $('#'+value).val(data.url);
  70. }
  71. } else {
  72. layer.msg(data.message, {icon: 4});
  73. //$("#"+file.id).find('.data').html("<font color='red'>"+data.message+"</font>");
  74. return false;
  75. }
  76. }
  77. ,error: function(){
  78. //请求异常回调
  79. }
  80. });
  81. });
  82. }
  83. function picDel(e,v,s)
  84. {
  85. if (confirm('确定要删除图片吗?')) {
  86. if (s == 1) {
  87. e.hide();
  88. $("#" + v).val('');
  89. } else {
  90. if (upload_pic.length <= 0 && $("#" + v).val()) {
  91. upload_pic = $("#" + v).val().split(',');
  92. }
  93. var p = e.find('img').attr('src');
  94. e.remove();
  95. upload_pic.remove(p);
  96. $("#" + v).val(upload_pic.join(','));
  97. }
  98. }
  99. }
  100. function fileDel(e,v)
  101. {
  102. if(confirm('确定要删除文件吗?'))
  103. {
  104. if(upload_file.length <= 0 && $("#" + v).val())
  105. {
  106. upload_file = $("#" + v).val().split(',');
  107. }
  108. var parent = e.parent();
  109. var p = parent.find('a').eq(0).attr('href');
  110. parent.remove();
  111. upload_file.remove(p);
  112. //console.info(pic);
  113. $("#" + v).val(upload_file.join(','));
  114. }
  115. }