Validate if file type is allowed

JS
S
JavaScript

Simple script to check if user uploaded file has an allowed type

1enum AllowedFileTypes {
2  pdf = 'application/pdf',
3  jpg = 'img/jpeg',
4  jpg1 = 'image/jpeg',
5  docx = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
6  docx2 = 'application/msword',
7  xlsx = 'application/vnd.ms-excel',
8  xlsx1 = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
9}
10
11  validateFileType(fileType: string) {
12    let isAllowed = false;
13    const values = Object.values(AllowedFileTypes);
14    if (values.includes(fileType as unknown as AllowedFileTypes)) {
15      isAllowed = true;
16    }
17    return isAllowed;
18  }

Created on 11/6/2022