file-utils.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * 文件处理工具类
  3. * 提供文件读取、下载等通用功能
  4. */
  5. (function(window) {
  6. 'use strict';
  7. class FileUtils {
  8. /**
  9. * 读取文件内容为DataURL
  10. * @param {File} file - 要读取的文件对象
  11. * @param {Function} callback - 回调函数,接收读取的数据
  12. */
  13. static readAsDataURL(file, callback) {
  14. const reader = new FileReader();
  15. reader.onload = function(e) {
  16. callback(e.target.result);
  17. };
  18. reader.onerror = function(e) {
  19. console.error('文件读取失败:', e);
  20. callback(null, e);
  21. };
  22. reader.readAsDataURL(file);
  23. }
  24. /**
  25. * 读取文件内容为文本
  26. * @param {File} file - 要读取的文件对象
  27. * @param {Function} callback - 回调函数,接收读取的文本
  28. */
  29. static readAsText(file, callback) {
  30. const reader = new FileReader();
  31. reader.onload = function(e) {
  32. callback(e.target.result);
  33. };
  34. reader.onerror = function(e) {
  35. console.error('文件读取失败:', e);
  36. callback(null, e);
  37. };
  38. reader.readAsText(file);
  39. }
  40. /**
  41. * 下载数据为文件
  42. * @param {string} content - 要下载的内容
  43. * @param {string} fileName - 文件名
  44. * @param {string} mimeType - MIME类型
  45. */
  46. static download(content, fileName, mimeType) {
  47. // 创建blob对象
  48. const blob = new Blob([content], { type: mimeType });
  49. // 创建用于下载的元素
  50. const link = document.createElement('a');
  51. link.href = URL.createObjectURL(blob);
  52. link.download = fileName;
  53. // 触发点击
  54. document.body.appendChild(link);
  55. link.click();
  56. // 清理
  57. document.body.removeChild(link);
  58. setTimeout(() => {
  59. URL.revokeObjectURL(link.href);
  60. }, 100);
  61. }
  62. /**
  63. * 下载DataURL为文件
  64. * @param {string} dataUrl - 数据URL
  65. * @param {string} fileName - 文件名
  66. */
  67. static downloadDataURL(dataUrl, fileName) {
  68. const link = document.createElement('a');
  69. link.href = dataUrl;
  70. link.download = fileName;
  71. // 触发点击
  72. document.body.appendChild(link);
  73. link.click();
  74. // 清理
  75. document.body.removeChild(link);
  76. }
  77. /**
  78. * 从URL获取文件名
  79. * @param {string} url - URL
  80. * @returns {string} 文件名
  81. */
  82. static getFileNameFromUrl(url) {
  83. try {
  84. const urlObj = new URL(url);
  85. const pathSegments = urlObj.pathname.split('/');
  86. return pathSegments[pathSegments.length - 1] || 'downloaded-file';
  87. } catch (e) {
  88. return 'downloaded-file';
  89. }
  90. }
  91. /**
  92. * 格式化字节大小为可读的字符串
  93. * @param {number} bytes - 字节数
  94. * @returns {string} 格式化后的字符串
  95. */
  96. static formatFileSize(bytes) {
  97. if (isNaN(bytes)) {
  98. return '未知大小';
  99. }
  100. const units = ['B', 'KB', 'MB', 'GB', 'TB'];
  101. let unitIndex = 0;
  102. let size = bytes;
  103. while (size >= 1024 && unitIndex < units.length - 1) {
  104. size /= 1024;
  105. unitIndex++;
  106. }
  107. return size.toFixed(2) + ' ' + units[unitIndex];
  108. }
  109. }
  110. window.FileUtils = FileUtils;
  111. })(window);