upload.js 3.2 KB

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