Implement file upload validation (types and size limit)

This commit is contained in:
Chneemann 2024-09-20 04:45:52 +02:00
parent 21be798e30
commit 6e27b9e8cb
2 changed files with 21 additions and 2 deletions

View file

@ -25,8 +25,9 @@
name="file"
autocomplete="off"
style="display: none"
(change)="onFileChange($event)"
(change)="handleFileSelection($event)"
multiple
accept=".png,.gif,.jpg,.jpeg,.pdf,.doc,.docx,.mp3,.wav,.mp4,.wmv,.avi"
/>
</div>
<textarea

View file

@ -169,10 +169,28 @@ export class ChatMsgBoxComponent {
'video/x-ms-wmv': 4,
'video/avi': 4,
};
return this.getFileIcons[fileTypeMap[fileInfo.type] ?? 5];
}
/**
* Processes the file selection and checks whether file types and file sizes meet the requirements.
* @param {Event} event - The file input change event that contains the uploaded files.
*/
handleFileSelection(event: any) {
const files = event.target.files;
const maxSize = 1 * 1024 * 1024; // 3 MB in Bytes
for (let i = 0; i < files.length; i++) {
const file = files[i];
// Prüfen der Dateigröße
if (file.size > maxSize) {
this.fileSizeError = false;
}
this.fileSizeError = true;
}
}
/**
* Deletes the selected file.
* @param file The file to be deleted.