uploadImg.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. $(function () {
  2. setTimeout(uploaderInput,1000);
  3. function uploaderInput(){
  4. // 允许上传的图片类型
  5. var allowTypes = ['image/jpg', 'image/jpeg', 'image/png', 'image/gif'];
  6. var maxSize = 1024 * 1024*20; // 10240KB,也就是 10MB
  7. var maxWidth = 1900; // 图片最大宽度
  8. //
  9. var $gallery = $("#gallery"),
  10. $galleryImg = $("#galleryImg"),
  11. $uploaderInput = $("#uploaderInput"),
  12. $uploaderFiles = $("#uploaderFiles");
  13. $uploaderInput.on("change",function (e) {
  14. files = e.target.files;
  15. // 如果没有选中文件,直接返回
  16. if (files.length === 0) {
  17. return;
  18. }
  19. for (var i = 0, len = files.length; i < len; ++i) {
  20. var file = files[i];
  21. var imgID = genGUID();
  22. var reader = new FileReader();
  23. var fileType = file.type;
  24. // 如果类型不在允许的类型范围内
  25. if (allowTypes.indexOf(file.type) === -1) {
  26. $.toast('该类型不允许上传' + fileType);
  27. continue;
  28. }
  29. if (file.size > maxSize) {
  30. $.toast("图片太大,不允许上传");
  31. continue;
  32. }
  33. // if ($('.weui-uploader__file').length >= maxCount) {
  34. // $.toast('最多只能上传' + maxCount + '张图片', "forbidden");
  35. // return;
  36. // }
  37. var base64;
  38. reader.onload = function (e) {
  39. $.showLoading("上传中");
  40. var img = new Image();
  41. img.onload = function () {
  42. // 不要超出最大宽度
  43. var w = Math.min(maxWidth, img.width);
  44. // 高度按比例计算
  45. var h = img.height * (w / img.width);
  46. var canvas = document.createElement('canvas');
  47. var ctx = canvas.getContext('2d');
  48. // 设置 canvas 的宽度和高度
  49. canvas.width = w;
  50. canvas.height = h;
  51. ctx.drawImage(img, 0, 0, w, h);
  52. base64 = canvas.toDataURL(fileType,0.6); //0.6指的是压缩60%
  53. // 插入到预览区
  54. // $uploaderFiles.find('.upload_imgs').before($(tmpl.replace('#url#', base64).replace('#ImgID#', imgID)));
  55. // var num = $('.weui-uploader__file').length;
  56. // $('#uploadCount').text(num);
  57. // 模拟上传进度
  58. // var progress = 0;
  59. // function uploading() {
  60. // $uploaderFiles.find('.weui_uploader_status_content').text(++progress + '%');
  61. // if (progress < 100) {
  62. // setTimeout(uploading, 30);
  63. // } else {
  64. // $uploaderFiles.removeClass('weui_uploader_status').find('.weui_uploader_status_content').remove();//清除上传进度图标
  65. // }
  66. // }
  67. // setTimeout(uploading, 30);
  68. };
  69. img.src = e.target.result;
  70. var FormDatas = new FormData();
  71. FormDatas.append("file", file);
  72. //这里实现上传
  73. $.ajax({
  74. url: uploadUrl,
  75. type: 'POST',
  76. data: FormDatas,
  77. contentType: false,
  78. processData: false,
  79. success: function (res) {
  80. if (res.code === '200') {
  81. var str = `
  82. <li class="weui-uploader__file" img-Url="${res.data.fullURL}" id="${imgID}" style="background-image:url(${base64})"><div class="weui_uploader_status_content">0%</div></li>`;
  83. $uploaderFiles.find('.upload_imgs').before(str);
  84. var num = $('.weui-uploader__file').length;
  85. $('#uploadCount').text(num);
  86. var progress = 0;
  87. function uploading() {
  88. $uploaderFiles.find('.weui_uploader_status_content').text(++progress + '%');
  89. if (progress < 100) {
  90. setTimeout(uploading, 30);
  91. } else {
  92. $uploaderFiles.removeClass('weui_uploader_status').find('.weui_uploader_status_content').remove();//清除上传进度图标
  93. $.toast("上传成功")
  94. $.hideLoading();
  95. }
  96. }
  97. setTimeout(uploading, 30);
  98. }else {
  99. $.toast("上传失败",'forbidden');
  100. $.hideLoading();
  101. }
  102. },
  103. error:function () {
  104. $.toast("上传失败,请检查网络!",'forbidden');
  105. $.hideLoading();
  106. }
  107. })
  108. };
  109. reader.readAsDataURL(file);
  110. }
  111. });
  112. function genGUID() {
  113. var G1 = (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1) + (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  114. var G2 = (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1) + (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  115. return (G1 + G2);
  116. }
  117. var index; //第几张图片
  118. $uploaderFiles.on("click", ".weui-uploader__file", function () {
  119. index = $(this).index();
  120. $galleryImg.attr("style", this.getAttribute("style"));
  121. $gallery.fadeIn(100);
  122. });
  123. $gallery.on("click", function () {
  124. $gallery.fadeOut(100);
  125. });
  126. //删除图片
  127. $(".weui-gallery__del").click(function () {
  128. $uploaderFiles.find("li").eq(index).remove();
  129. var num = $('.weui-uploader__file').length;
  130. $('#uploadCount').text(num);
  131. });
  132. }
  133. });