upload.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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: {'key' : key}
  17. ,field: 'file'
  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. }
  57. //$("#"+file.id).find('.data').html('上传完毕');
  58. $('#'+value).val(data.url);
  59. }
  60. } else {
  61. //$("#"+file.id).find('.data').html("<font color='red'>"+data.message+"</font>");
  62. return false;
  63. }
  64. }
  65. ,error: function(){
  66. //请求异常回调
  67. }
  68. });
  69. });
  70. }
  71. function picDel(e,v)
  72. {
  73. if(confirm('确定要删除图片吗?'))
  74. {
  75. if(upload_pic.length <= 0 && $("#" + v).val())
  76. {
  77. upload_pic = $("#" + v).val().split(',');
  78. }
  79. var p = e.find('img').attr('src');
  80. e.remove();
  81. upload_pic.remove(p);
  82. //console.info(pic);
  83. $("#" + v).val(upload_pic.join(','));
  84. }
  85. }
  86. function fileDel(e,v)
  87. {
  88. if(confirm('确定要删除文件吗?'))
  89. {
  90. if(upload_file.length <= 0 && $("#" + v).val())
  91. {
  92. upload_file = $("#" + v).val().split(',');
  93. }
  94. var parent = e.parent();
  95. var p = parent.find('a').eq(0).attr('href');
  96. parent.remove();
  97. upload_file.remove(p);
  98. //console.info(pic);
  99. $("#" + v).val(upload_file.join(','));
  100. }
  101. }