Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR TYPESCRIPT

file reader with promise

export const fileReader = (file: File) => {
    const reader = new FileReader();

    if(!file) return;

    return new Promise((resolve, reject) => {
        reader.onerror = () => {
            reader.abort();
            reject(new DOMException("Problem parsing input file."));
        }

        reader.readAsDataURL(file);
        reader.onload = (e) => {
            resolve(e.target?.result);
        }
    })
}
 
PREVIOUS NEXT
Tagged: #file #reader #promise
ADD COMMENT
Topic
Name
2+8 =