file.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // 媒体名称
  2. export const mediaTypes = {
  3. image: "图片",
  4. video: "视频",
  5. audio: "音频",
  6. };
  7. // 媒体扩展类型
  8. export const mediaMimes = {
  9. image: ["jpg", "png", "jpeg", "bmp", "gif"],
  10. audio: ["mp3", "aac", "ogg", "wav" /* , "m4a" */],
  11. video: ["mp4", "mov", "quicktime" /* ,"webm", "rmvb", "wmv" */], //ios:mov
  12. };
  13. // 媒体大小显示(MB)
  14. export const mediaMaxSize = {
  15. image: 10,
  16. video: 20,
  17. audio: 5,
  18. };
  19. /**
  20. * 获取媒体扩展类型
  21. * @param {Stirng} filename 文件名称
  22. */
  23. export const getMime = (filename) => {
  24. if (!filename || filename.indexOf(".") === -1) {
  25. return "";
  26. }
  27. return filename.split(".").pop().toLowerCase();
  28. };
  29. /**
  30. * 在路径中获取文件名
  31. * @param {*} path
  32. */
  33. export const getFilename = (path) => {
  34. const segment = (path || "").split("/");
  35. return segment[segment.length - 1];
  36. };
  37. /**
  38. * 检测媒体文件是否超过预设限制
  39. * @param {String} type 媒体类型
  40. * @param {Number} size 文件大小
  41. */
  42. export const checkSizeLimit = (type, size) => {
  43. size = size / 1024 / 1024;
  44. return size <= mediaMaxSize[type];
  45. };
  46. export const checkSizeLimitFree = (size, limit) => {
  47. size = size / 1024 / 1024;
  48. return size <= limit;
  49. };
  50. /**
  51. * 检测媒体类型
  52. * @param {String} type 媒体类型
  53. * @param {String} filename 文件名称
  54. */
  55. export const checkMediaMime = (type, filename) => {
  56. const mime = getMime(filename);
  57. const find = mediaMimes[type];
  58. if (!find) {
  59. return false;
  60. }
  61. return find.indexOf(mime) !== -1;
  62. };
  63. export const base64ToBlob = (base64) => {
  64. let arr = base64.split(","),
  65. mime = arr[0].match(/:(.*?);/)[1],
  66. bstr = atob(arr[1]),
  67. n = bstr.length,
  68. u8arr = new Uint8Array(n);
  69. while (n--) {
  70. u8arr[n] = bstr.charCodeAt(n);
  71. }
  72. return new Blob([u8arr], { type: mime });
  73. };
  74. export const base64ToDataURL = (base64) => {
  75. return window.URL.createObjectURL(base64ToBlob(base64));
  76. };
  77. export const blobToBase64 = function (blob) {
  78. return new Promise((resolve) => {
  79. var reader = new FileReader();
  80. reader.onload = function () {
  81. resolve(reader.result);
  82. };
  83. reader.readAsDataURL(blob);
  84. });
  85. };
  86. /**
  87. * 获取图片文件尺寸
  88. * @param {*} file
  89. */
  90. export const getImgWH = (file) => {
  91. return new Promise((resolve, reject) => {
  92. var reader = new FileReader();
  93. //读取图片文件
  94. let url = URL.createObjectURL(file);
  95. var image = new Image();
  96. //FileReader获得Base64字符串
  97. image.src = url;
  98. image.onload = function () {
  99. console.log("图片加载成功");
  100. //获得图片高宽
  101. var height = this.height;
  102. var width = this.width;
  103. resolve({
  104. height,
  105. width,
  106. });
  107. };
  108. image.onerror = function (e) {
  109. reject({ message: "loadFile Error" });
  110. };
  111. // reader.readAsDataURL(file);
  112. // reader.onload = function (e) {
  113. // //初始化JavaScript图片对象
  114. // var image = new Image();
  115. // //FileReader获得Base64字符串
  116. // image.src = e.target.result;
  117. // image.onload = function () {
  118. // console.error("图片加载成功");
  119. // //获得图片高宽
  120. // var height = this.height;
  121. // var width = this.width;
  122. // resolve({
  123. // height,
  124. // width,
  125. // });
  126. // };
  127. // image.onerror = function (e) {
  128. // console.error("图片加载失败",e);
  129. // reject({
  130. // height: 0,
  131. // width: 0,
  132. // });
  133. // };
  134. // };
  135. });
  136. };
  137. /**
  138. * 转化字节单位
  139. * @param {*} file
  140. */
  141. export const changeByteUnit = (x) => {
  142. const units = ["KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
  143. let l = 0,
  144. n = parseInt(x, 10) || 0;
  145. while (n >= 1024 && ++l) {
  146. n = n / 1024;
  147. }
  148. return n.toFixed(n < 10 && l > 0 ? 2 : 0) + units[l];
  149. };