I'm using .net core to upload an image file. The file is converted to a MemoryStream and then sent to a service which will validate the file. Currently I do that with something like this:
MemoryStream stream = GetMemoryStream();
var fileSize = stream.length;
Now I want to ensure that the image is not larger than 512 kb. My problem is that I'm not sure what value to use to convert to bytes, 1024 or 1000? Should I use the decimal or binary values when listing length from MemoryStream?
Which scenario is correct?
Scenario A
if(fileSize > 512000) return false;
Scenario B
if(fileSize > 524288) return false;
EDIT:
When I look at properties of an image on my desktop and it says 512KB. Which scenario did the computer use to calculate that size?
MemoryStream.Length
returns the length of the contained data in bytes.
Therefore, your validation completely depends on your definition of 512 kb
. So both of your scenarios are somewhat true; in the first one you assume that 1 kb = 1000 bytes
, and in the second one you assume that 1 kb = 1024 bytes
. It is up to you to decide which fits your application better.
Regarding your edit, the value used for a kilobyte on your computer depends on the OS you are using. E.g., on Windows it is 1024 bytes.