havit / Havit.Blazor

Free Bootstrap 5 components for ASP.NET Blazor + optional enterprise-level stack for Blazor development (gRPC code-first, layered architecture, localization, auth, ...)
https://havit.blazor.eu
MIT License
490 stars 67 forks source link

[HxFileInput] max size validation #98

Closed petrkasnal closed 2 years ago

petrkasnal commented 2 years ago

Hello, is any posibility to add validation for max size in FileInput?

Thank you

hakenr commented 2 years ago

You can use the MaxFileSize parameter https://havit.blazor.eu/components/HxInputFile#LimitFileSizeAndTypes

<HxInputFile MaxFileSize="500000" />
petrkasnal commented 2 years ago

@hakenr thank you for response. I use this, but its not return me any validation. Do you have any example of this please? Thank you

hakenr commented 2 years ago

The HxInputFile component does not interact with <EditForm /> (EditContext) and is not intended to take part in the classic form-validation workflow (there is no "value" to bind to and validate).

Currently, trying to upload a file bigger than the MaxFileSize limit emits a warning to the console, does not upload such file and signals such file with OnFileUploaded callback where the status code is set to 413 with a explanatory response message.

https://github.com/havit/Havit.Blazor/blob/18ca3a804043fd2e0a9dabfcbf6d42787260292a/Havit.Blazor.Components.Web/wwwroot/HxInputFileCore.js#L33-L45

If you need a custom visual signalization of such files, you have to either use the OnChange callback and check the file sizes on your own, or use the OnFileUploaded callback where you will check for 413 and act accordingly.

petrkasnal commented 2 years ago

I understand, so the solution is to do the validation manually. Which I didn't want to do, but I understand that it can't be done any other way, because it's not a bind in the editform. Thank you very much :)