index.uts 2.5 KB

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