dwz.file.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * Created by huihuazhang on 2016/4/27.
  3. * 基于HTML5 文件上传的核心脚本
  4. * http://www.w3.org/TR/html-markup/input.file.html
  5. */
  6. (function($){
  7. function readAsDataURL(img, file, maxW, maxH){
  8. // Using FileReader to display the image content
  9. var reader = new FileReader();
  10. reader.onload = (function(aImg) {
  11. return function(e) {
  12. aImg.src = e.target.result;
  13. var width = aImg.naturalWidth,
  14. height = aImg.naturalHeight;
  15. aImg.setAttribute('data-width', width);
  16. aImg.setAttribute('data-height', height);
  17. if (maxW && maxH) {
  18. if (width/maxW > height/maxH) {
  19. aImg.setAttribute('height', maxH);
  20. } else {
  21. aImg.setAttribute('width', maxW);
  22. }
  23. }
  24. };
  25. })(img);
  26. reader.readAsDataURL(file);
  27. }
  28. function previewUploadImg($uploadWrap, files, maxW, maxH) {
  29. var $previewElem = $('<div class="thumbnail"></div>').appendTo($uploadWrap);
  30. var file = files[0];
  31. if (!file) {return false;}
  32. if (!file.type.match(/image.*/)) {
  33. throw "File Type must be an image";
  34. }
  35. var img = document.createElement("img");
  36. img.file = file;
  37. $previewElem.empty().append(img);
  38. // if ($previewElem.find('.edit-icon').size() == 0) {
  39. // $previewElem.append('<span class="edit-icon"></span>');
  40. // }
  41. if ($previewElem.find('.del-icon').size() == 0) {
  42. $('<a class="del-icon"></a>').appendTo($previewElem).click(function(event){
  43. $previewElem.remove();
  44. $uploadWrap.find('input[type=file]').val('');
  45. });
  46. }
  47. readAsDataURL(img, file, maxW, maxH);
  48. }
  49. // multiple
  50. function previewUploadImg2($uploadWrap, files, maxW, maxH) {
  51. var rel = $uploadWrap.attr('rel');
  52. var $previewElem = $(rel);
  53. $previewElem.empty();
  54. for (var index=0; index<files.length; index++) {
  55. var file = files[index];
  56. var $thumb = $('<li class="thumbnail"></li>');
  57. var img = document.createElement("img");
  58. img.file = file;
  59. $thumb.append(img);
  60. $previewElem.append($thumb);
  61. readAsDataURL(img, file, maxW, maxH);
  62. }
  63. }
  64. $.fn.extend({
  65. /**
  66. * 选择上传图片缩略图列表预览
  67. * @param options
  68. */
  69. previewUploadImg: function(options){
  70. var op = $.extend({maxW:80, maxH:80}, options);
  71. return this.each(function(){
  72. var $uploadWrap = $(this);
  73. var $inputFiles = $uploadWrap.find('input[type=file]');
  74. $inputFiles.each(function(index){
  75. var $inputFile = $(this).css({left:(op.maxW*index)+'px'});
  76. $inputFile.on('change', function () {
  77. var files = this.files;
  78. if (this.multiple) {
  79. previewUploadImg2($uploadWrap, files, op.maxW, op.maxH);
  80. } else {
  81. previewUploadImg($uploadWrap, files, op.maxW, op.maxH);
  82. }
  83. });
  84. });
  85. var $delIcon = $uploadWrap.find('.del-icon');
  86. if ($delIcon) { // 删除服务器上的图片
  87. $delIcon.click(function(event){
  88. $.ajax({
  89. type: 'GET',
  90. url:$delIcon.attr('href'),
  91. dataType:"json",
  92. cache: false,
  93. success: function(json){
  94. DWZ.ajaxDone(json);
  95. if (json[DWZ.keys.statusCode] == DWZ.statusCode.ok){
  96. $uploadWrap.find('div.thumbnail').remove();
  97. $uploadWrap.find('input[type=file]').val('');
  98. }
  99. },
  100. error: DWZ.ajaxError
  101. });
  102. return false;
  103. });
  104. }
  105. });
  106. }
  107. });
  108. DWZ.regPlugins.push(function($p){
  109. $("div.upload-wrap", $p).previewUploadImg();
  110. });
  111. })(jQuery);