upload.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. $(function () {
  2. setTimeout(fileToUploads,1000);
  3. function fileToUploads() {
  4. var $fileToUploads = $("#fileToUploads"),
  5. $uploaderFiles = $("#uploaderFiles");
  6. $fileToUploads.on("change", function (e) {
  7. console.log('000000file')
  8. files = e.target.files;
  9. // 如果没有选中文件,直接返回
  10. if (files.length === 0) {
  11. return;
  12. }
  13. for (var i = 0, len = files.length; i < len; ++i) {
  14. var file = files[i];
  15. var imgID = genGUID();
  16. var reader = new FileReader();
  17. var fileType = file.type;
  18. // console.log('file.type',file.type)
  19. // // 如果类型不在允许的类型范围内
  20. // if (allowTypes.indexOf(file.type) === -1) {
  21. // console.log(allowTypes.indexOf(file.type)) ;
  22. // $.toast('该类型不允许上传' + fileType);
  23. // continue;
  24. // }
  25. var base64;
  26. reader.onload = function (e) {
  27. $.showLoading("上传中");
  28. // 插入到预览区
  29. // $uploaderFiles.find('.upload_imgs').before($(tmpl.replace('#url#', base64).replace('#ImgID#', imgID)));
  30. // var num = $('.weui-uploader__file').length;
  31. // $('#uploadCount').text(num);
  32. // 模拟上传进度
  33. // var progress = 0;
  34. // function uploading() {
  35. // $uploaderFiles.find('.weui_uploader_status_content').text(++progress + '%');
  36. // if (progress < 100) {
  37. // setTimeout(uploading, 30);
  38. // } else {
  39. // $uploaderFiles.removeClass('weui_uploader_status').find('.weui_uploader_status_content').remove();//清除上传进度图标
  40. // }
  41. // }
  42. // setTimeout(uploading, 30);
  43. var FormDatas = new FormData();
  44. FormDatas.append("file", file);
  45. //这里实现上传
  46. $.ajax({
  47. url: uploadUrl,
  48. type: 'POST',
  49. data: FormDatas,
  50. contentType: false,
  51. processData: false,
  52. success: function (res) {
  53. if (res.code === "200") {
  54. if (res.data.fileExt == 'zip' || res.data.fileExt == 'rar') {
  55. var str = `
  56. <li class="flex-con" file-Url="${res.data.fullURL}" file-Name="${res.data.fileName}">
  57. <img src="image/zip.png" alt="">
  58. <span>${res.data.fileName}</span>
  59. <i class="close_s delete_file weui-icon-cancel"></i>
  60. </li>
  61. `;
  62. $('.fileList .lastUpload').before(str);
  63. } else if (res.data.fileExt == 'pdf') {
  64. var str = `
  65. <li class="flex-con" file-Url="${res.data.fullURL}" file-Name="${res.data.fileName}">
  66. <img src="image/pdf.png" alt="">
  67. <span>${res.data.fileName}</span>
  68. <i class="close_s delete_file weui-icon-cancel"></i>
  69. </li>
  70. `;
  71. $('.fileList .lastUpload').before(str);
  72. } else if (res.data.fileExt == 'pptx' || res.data.fileExt == 'ppt') {
  73. var str = `
  74. <li class="flex-con" file-Url="${res.data.fullURL}" file-Name="${res.data.fileName}">
  75. <img src="image/ppt.png" alt="">
  76. <span>${res.data.fileName}</span>
  77. <i class="close_s delete_file weui-icon-cancel"></i>
  78. </li>
  79. `;
  80. $('.fileList .lastUpload').before(str);
  81. } else if (res.data.fileExt == 'doc') {
  82. var str = `
  83. <li class="flex-con" file-Url="${res.data.fullURL}" file-Name="${res.data.fileName}">
  84. <img src="image/doc.png" alt="">
  85. <span>${res.data.fileName}</span>
  86. <i class="close_s delete_file weui-icon-cancel"></i>
  87. </li>
  88. `;
  89. $('.fileList .lastUpload').before(str);
  90. } else if (res.data.fileExt == 'xlsx') {
  91. var str = `
  92. <li class="flex-con" file-Url="${res.data.fullURL}" file-Name="${res.data.fileName}">
  93. <img src="image/lsx.png" alt="">
  94. <span>${res.data.fileName}</span>
  95. <i class="close_s delete_file weui-icon-cancel"></i>
  96. </li>
  97. `;
  98. $('.fileList .lastUpload').before(str);
  99. } else if (res.data.fileExt == 'png' || res.data.fileExt == 'jpg' || res.data.fileExt == 'jpeg' || res.data.fileExt == 'JPG' || res.data.fileExt == 'JPEG' || res.data.fileExt == 'PNG') {
  100. var str = `
  101. <li class="flex-con" file-Url="${res.data.fullURL}" file-Name="${res.data.fileName}">
  102. <img src="${res.data.fullURL}" alt="">
  103. <i class="close_s delete_img weui-icon-cancel"></i>
  104. </li>
  105. `;
  106. $('.fileList .lastUploadimg').before(str);
  107. } else if (res.data.fileExt) {
  108. }
  109. $.toast('上传成功');
  110. $.hideLoading();
  111. } else {
  112. $.toast('上传失败,请检查网络','forbidden');
  113. $.hideLoading();
  114. }
  115. },
  116. error:function () {
  117. $.toast("上传失败,请检查网络!",'forbidden');
  118. $.hideLoading();
  119. }
  120. })
  121. };
  122. reader.readAsDataURL(file);
  123. }
  124. });
  125. function genGUID() {
  126. var G1 = (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1) + (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  127. var G2 = (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1) + (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  128. return (G1 + G2);
  129. }
  130. }
  131. });