index.uts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // fileToBase64, fileToDataURL,dataURLToFile
  2. export function fileToBase64(filePath : string) {
  3. return new Promise((resolve, reject)=>{
  4. if(uni.canIUse('getFileSystemManager')){
  5. uni.getFileSystemManager().readFile({
  6. filePath: path,
  7. encoding: 'base64',
  8. success: (res) => {
  9. resolve(res.data)
  10. },
  11. fail: (error) => {
  12. console.error({ error, path })
  13. reject(error)
  14. }
  15. })
  16. } else {
  17. reject('fileToBase64:环境不支持')
  18. }
  19. })
  20. }
  21. export function fileToDataURL(filePath : string) {
  22. let extension = path.substring(path.lastIndexOf('.') + 1);
  23. const imageExtensions = ["jpg", "jpeg", "png", "gif", "bmp", "svg"];
  24. const isImageFile = imageExtensions.includes(extension.toLowerCase());
  25. let prefix = ''
  26. if (isImageFile) {
  27. prefix = 'image/';
  28. if(extension == 'svg') {
  29. extension += '+xml'
  30. }
  31. } else if (extension === 'pdf') {
  32. prefix = 'application/pdf';
  33. } else if (extension === 'txt') {
  34. prefix = 'text/plain';
  35. } else {
  36. // 添加更多文件类型的判断
  37. // 如果不是图片、PDF、文本等类型,可以设定默认的前缀或采取其他处理
  38. prefix = 'application/octet-stream';
  39. }
  40. return fileToBase64(filePath).then(res => `data:${prefix}${extension};base64,${res}`)
  41. }
  42. function getFileExtensionFromDataURL(dataURL : string) : string {
  43. const commaIndex = dataURL.indexOf(",");
  44. const mimeType = dataURL.substring(0, commaIndex).replace("data:", "").replace(";base64", "");
  45. const mimeTypeParts = mimeType.split("/");
  46. return mimeTypeParts[1];
  47. }
  48. function getPlatform():Uni {
  49. // #ifdef MP-WEIXIN
  50. return wx
  51. // #endif
  52. // #ifdef MP-BAIDU
  53. return swan
  54. // #endif
  55. // #ifdef MP-ALIPAY
  56. return my
  57. // #endif
  58. // #ifdef MP-JD
  59. return jd
  60. // #endif
  61. // #ifdef MP-QQ
  62. return qq
  63. // #endif
  64. // #ifdef MP-360
  65. return qh
  66. // #endif
  67. // #ifdef MP-KUAISHOU
  68. return ks
  69. // #endif
  70. // #ifdef MP-LARK||MP-TOUTIAO
  71. return tt
  72. // #endif
  73. // #ifdef MP-DINGTALK
  74. return dd
  75. // #endif
  76. // #ifdef QUICKAPP-WEBVIEW || QUICKAPP-WEBVIEW-UNION || QUICKAPP-WEBVIEW-HUAWEI
  77. return qa
  78. // #endif
  79. return uni
  80. }
  81. export function dataURLToFile(dataURL : string, filename : NullableString = null) {
  82. return new Promise((resolve, reject) => {
  83. const name = filename ?? `${Date.now()}.${getFileExtensionFromDataURL(dataURL)}`;
  84. const commaIndex = dataURL.indexOf(",");
  85. const base64 = dataURL.substring(commaIndex + 1);
  86. const platform = getPlatform()
  87. const filePath = `${platform.env.USER_DATA_PATH}/${name}`;
  88. fs.writeFile({
  89. filePath,
  90. data: base64,
  91. encoding: 'base64',
  92. success() {
  93. resolve(filePath)
  94. },
  95. fail(err) {
  96. reject(err)
  97. }
  98. })
  99. })
  100. }