According to the ASP.NET Core Blazor file uploads:
OpenReadStream enforces a maximum size in bytes of its Stream. Reading one file or multiple files larger than 500 KB results in an exception. This limit prevents developers from accidentally reading large files into memory. The
maxAllowedSizeparameter of OpenReadStream can be used to specify a larger size if required.
With this example:
const long maxAllowedSize = 3L * 1024L * 1024L; //3 MB limit
await using FileStream fs = new(path, FileMode.Create);
await browserFile.OpenReadStream(maxAllowedSize).CopyToAsync(fs);
If I can use OpenReadStream to specify the maxAllowedSize, I can avoid file sizes higher than the specified size.
Now, how should I get the image file dimension and validate it to prevent it from being uploaded?
I need to allow only image dimensions between 330x330 and 5000x5000, and I need to manage this programmatically to inform the user based on its validity.
Note that I'm using the InputFileChangeEventArgs for this instance.
I would greatly appreciate any help!
input type="file"nor the HTTP headers contain image metadata. Something has to read that image metadata. The browser can detect the MIME type but doesn't try to read anything else.