11/**
22 * Reads an image (or other type) file as data URL in a promise way,
33 * so you can use await.
4- * If other kind of file is sent, this function will read it anyway
5- * and will return a string that contains the URL representation
4+ * It will return a string that contains the URL representation
65 * @param file File or Blob object
76 * @returns data URL of the file
87 */
9- export const readAsDataURL = ( file : File | Blob ) : Promise < string | undefined > => {
8+ export const readAsDataURL = ( file : File | Blob , onProgress ?: Function , onError ?: Function ) : Promise < string | undefined > => {
109 return new Promise < string | undefined > ( ( resolve , reject ) => {
1110 try {
1211 const reader = new FileReader ( ) ;
12+ reader . onprogress = ( ) => {
13+ onProgress ?.( ) ;
14+ }
15+ reader . onerror = function ( ) {
16+ onError ?.( ) ;
17+ }
1318 reader . onload = function ( ) {
1419 resolve ( reader . result as string ) ;
1520 }
@@ -30,13 +35,19 @@ export const readAsDataURL = (file: File | Blob): Promise<string | undefined> =>
3035 * @param encoding The type of encoding such as "base64"
3136 * @returns data text of the file
3237 */
33- export const readAsText = ( file : File | Blob , encoding ?: string ) : Promise < string | undefined > => {
38+ export const readAsText = ( file : File | Blob , encoding ?: string , onProgress ?: Function , onError ?: Function ) : Promise < string | undefined > => {
3439 return new Promise < string | undefined > ( ( resolve , reject ) => {
3540 try {
3641 const reader = new FileReader ( ) ;
3742 reader . onload = function ( ) {
3843 resolve ( reader . result as string ) ;
3944 }
45+ reader . onprogress = ( ) => {
46+ onProgress ?.( ) ;
47+ }
48+ reader . onerror = function ( ) {
49+ onError ?.( ) ;
50+ }
4051 reader . readAsText ( file , encoding ? encoding : "base64" ) ;
4152 } catch ( error ) {
4253 reject ( undefined ) ;
@@ -52,13 +63,19 @@ export const readAsText = (file: File | Blob, encoding?: string): Promise<string
5263 * @param encoding The type of encoding such as "base64"
5364 * @returns raw binary data of the file
5465 */
55- export const readAsBinaryString = ( file : File | Blob ) : Promise < string | undefined > => {
66+ export const readAsBinaryString = ( file : File | Blob , onProgress ?: Function , onError ?: Function ) : Promise < string | undefined > => {
5667 return new Promise < string | undefined > ( ( resolve , reject ) => {
5768 try {
5869 const reader = new FileReader ( ) ;
5970 reader . onload = function ( ) {
6071 resolve ( reader . result as string ) ;
6172 }
73+ reader . onprogress = ( ) => {
74+ onProgress ?.( ) ;
75+ }
76+ reader . onerror = function ( ) {
77+ onError ?.( ) ;
78+ }
6279 reader . readAsBinaryString ( file ) ;
6380 } catch ( error ) {
6481 reject ( undefined ) ;
@@ -72,13 +89,19 @@ export const readAsBinaryString = (file: File | Blob): Promise<string | undefine
7289 * @param encoding The type of encoding such as "base64"
7390 * @returns ArrayBuffer representation of the file
7491 */
75- export const readAsArrayBuffer = ( file : File | Blob ) : Promise < string | undefined > => {
92+ export const readAsArrayBuffer = ( file : File | Blob , onProgress ?: Function , onError ?: Function ) : Promise < string | undefined > => {
7693 return new Promise < string | undefined > ( ( resolve , reject ) => {
7794 try {
7895 const reader = new FileReader ( ) ;
7996 reader . onload = function ( ) {
8097 resolve ( reader . result as string ) ;
8198 }
99+ reader . onprogress = ( ) => {
100+ onProgress ?.( ) ;
101+ }
102+ reader . onerror = function ( ) {
103+ onError ?.( ) ;
104+ }
82105 reader . readAsArrayBuffer ( file ) ;
83106 } catch ( error ) {
84107 reject ( undefined ) ;
0 commit comments