I'm working with angular 5 reactive form. In that I have one browse field, On change event of that browse button I'm calling a function, in that file validation is checked. If file is invalid, I'm setting an error manually to that field.
Now what I'm expecting is when file input error is set. Error message should display in DOM. It's working fine with Chrome, Firefox and Android devices. But It is not working in IE and IOS.
HTML CODE
<div class="form-group" [ngClass]="{'has-error': hasError('profileimage')}">
<label class="control-label"> Upload Profile Image...</label>
<input id="profileimage"
(change)="fileUpload($event.target.files,'profileimage')"
type="file"
formControlName="profileimage">
<span class="is-error" *ngIf="checkValidImageFormat('profileimage')">
Only file with extensions are allowed: .png, .jpeg, .jpg .</span>
<span class="is-error" *ngIf="checkValidImageSize('profileimage')">File must be less than 1 MB .
</span>
</div>
Component.ts
fileUpload(files: FileList, field) {
this.profileimage = files[0];
let extension = this.profileimage.name.match(/(?:\.([^.]+))?$/)[1];
//checking the extension of the file
if (extension.toLowerCase() === 'jpg' ||
extension.toLowerCase() === 'jpeg' ||
extension.toLowerCase() === 'png') {
var sizeInMB = (this.profileimage.size / (1024 * 1024)).toFixed(2);
//checking the file size should be less than 1 mb
if (!parseFloat(sizeInMB) < 1) {
this.contactForm.controls[field].setErrors({ 'invalid_size': true });
}
}
checkValidImageSize(fieldName) {
return this.contactForm.controls[fieldName].errors.invalid_size;
}
I have tried all change detection strategies (NgZone, ChangeDetectorRef etc.) for angular but none of them worked. Any help would be appreciated.