合同小程序前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.5 KiB

3 months ago
  1. // @ts-nocheck
  2. export * from '../interface'
  3. import { ProcessFileOptions, NullableString } from '../interface'
  4. function readFileAs(
  5. file : File | string,
  6. method : 'readAsDataURL' | 'readAsText' | 'readAsArrayBuffer' | 'readAsBinaryString'
  7. ) : Promise<string | ArrayBuffer> {
  8. try {
  9. return new Promise(async (resolve, reject) => {
  10. let blob : Blob | null = null;
  11. if (typeof file === 'string') {
  12. const response = await fetch(file);
  13. if (!response || !response.ok) {
  14. return reject('readFileAs null');
  15. }
  16. blob = await response!.blob();
  17. }
  18. const reader = new FileReader();
  19. reader[method](blob ?? file);
  20. reader.onload = () => {
  21. resolve(reader.result);
  22. };
  23. reader.onerror = (error) => {
  24. reject(error);
  25. };
  26. });
  27. } catch (error) {
  28. return Promise.reject(error)
  29. }
  30. }
  31. export function fileToBase64(filePath : string | File) : Promise<string> {
  32. return readFileAs(filePath, 'readAsDataURL').then(result => (result as string).split(',')?.[1])
  33. }
  34. export function fileToDataURL(filePath : string | File) : Promise<string> {
  35. return readFileAs(filePath, 'readAsDataURL').then(result => (result as string));
  36. }
  37. export function dataURLToFile(dataURL : string, filename : NullableString = null) : Promise<string> {
  38. return new Promise((resolve, reject)=>{
  39. // mime类型
  40. let mimeString = dataURL.split(',')[0].split(':')[1].split(';')[0];
  41. //base64 解码
  42. let byteString = atob(dataURL.split(',')[1]);
  43. if (byteString == null) {
  44. return reject('dataURLToFile: 解析失败')
  45. };
  46. //创建缓冲数组
  47. let arrayBuffer = new ArrayBuffer(byteString.length);
  48. //创建视图
  49. let intArray = new Uint8Array(arrayBuffer);
  50. for (let i = 0; i < byteString.length; i++) {
  51. intArray[i] = byteString.charCodeAt(i);
  52. }
  53. // @ts-ignore
  54. const blob = new Blob([intArray], { type: mimeString });
  55. resolve(URL.createObjectURL(blob))
  56. })
  57. }
  58. export function processFile(options: ProcessFileOptions){
  59. if(options.type == 'toBase64'){
  60. fileToBase64(options.path).then(res =>{
  61. options.success?.(res)
  62. options.complete?.(res)
  63. }).catch(err =>{
  64. options.fail?.(err)
  65. options.complete?.(err)
  66. })
  67. } else if(options.type == 'toDataURL'){
  68. fileToDataURL(options.path).then(res =>{
  69. options.success?.(res)
  70. options.complete?.(res)
  71. }).catch(err =>{
  72. options.fail?.(err)
  73. options.complete?.(err)
  74. })
  75. } else if(options.type == 'toFile'){
  76. dataURLToFile(options.path).then(res =>{
  77. options.success?.(res)
  78. options.complete?.(res)
  79. }).catch(err =>{
  80. options.fail?.(err)
  81. options.complete?.(err)
  82. })
  83. }
  84. }