upload.js 3.4 KB

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