Check size of uploaded file in mb
Since you are given the size in bytes, you need to divide by 1048576
(i.e. 1024 * 1024
):
var fileSize = imageFile.ContentLength;
if ((fileSize / 1048576.0) > 10)
{
// image is too large
}
But the calculation is a bit easier to read if you pre-calculate the number of bytes in 10mb:
private const int TenMegaBytes = 10 * 1024 * 1024;
var fileSize = imageFile.ContentLength;
if ((fileSize > TenMegaBytes)
{
// image is too large
}
You can use this method to convert the bytes
you got to MB:
static double ConvertBytesToMegabytes(long bytes)
{
return (bytes / 1024f) / 1024f;
}
Prefixes for multiples of bytes (B):
1024 bytes = 1kilobyte
1024 kilobyte = 1megabyte
double ConvertBytesToMegabytes(long bytes)
{
return (bytes / 1024f) / 1024f;
}
var fileSize = imageFile.ContentLength;
if (ConvertBytesToMegabytes(fileSize ) > 10f)
{
// image is too large
}