Empty array from BinaryReader to UploadedFile in C #

Assume the following code:

Stream file = files[0].InputStream; var FileLen = files[0].ContentLength; var b = new BinaryReader(file); var bytes = b.ReadBytes(FileLen); 

If I upload a CSV file that is 10 records (257 bytes), BinaryReader fills the byte array with "0".

I also wrote a loop to go through the ReadByte method for BinaryReader, and in the first iteration of the loop, I got the following exception:

Unable to read beyond end of stream

When I expand the CSV file to 200 hundred records, everything works fine.

The question is why this happens on small files, and is there a workaround that allows binary reading of smaller files.

+4
source share
1 answer

I don’t know why, but when you use BinaryReader in a loaded stream, the initial position should be explicitly set.

 b.BaseStream.Position = 0; 
+11
source

Source: https://habr.com/ru/post/1338216/


All Articles