file-utils.js 995 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import getSize from '../3rd-libs/filesize';
  2. /**
  3. * 获取文件的尺寸
  4. */
  5. export const getFileSize = getSize;
  6. /**
  7. * 将URL转换为blob数据
  8. * @param {string} url - 地址
  9. */
  10. export async function url2Blob(url) {
  11. try {
  12. // 使用 fetch 获取资源
  13. const response = await fetch(url);
  14. // 检查响应是否成功(状态码在 200-299 之间)
  15. if (!response.ok) {
  16. throw new Error(`Network response was not ok: ${response.statusText}`);
  17. }
  18. // 将响应转换为 Blob 对象
  19. const blob = await response.blob();
  20. return blob;
  21. } catch (error) {
  22. console.error('There was a problem with the fetch operation:', error);
  23. throw error;
  24. }
  25. }
  26. /**
  27. * 根据文件名获取文件后缀名
  28. * @param {string} file_name
  29. * @returns
  30. */
  31. export const getFileSuffix = (file_name) => {
  32. if (!file_name) {
  33. return '';
  34. }
  35. const name_info_arr = file_name.split('.');
  36. const end_fix = name_info_arr[name_info_arr.length - 1];
  37. return end_fix;
  38. };