I have file uploader component and need to add size validator in it
This is the code for the validator
export const filesDimensionValidator = (maxWidth: number, maxHeight: number): ValidatorFn => (control: AbstractControl): ValidationErrors | null => { const value: IUploadFileControl = control.value; if (value == null) { return null; } for (const x of value.allFiles) { if (x.size > maxFileSize) { return { file_max_size: VALIDATION_LABEL_ATTACHMENT_MAX_SIZE }; } } return null; };
If the for loop x
parameter has type File
.
Only images here.
How to get the width and height of the image in this file?
Create an image using
Image
and then get the desired dimensions.You can use
file.type
to verify thatx
is an image:Try this:
edit
Get the size simultaneously: