9

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 ), the BinaryReader fills the array of bytes with "0".

I also wrote a loop to step through the ReadByte Method of the BinaryReader and in the first iteration of the loop, I received the following exception:

Unable to read beyond the end of the stream

When I increase the CSV file to 200 hundred records, everything worked just fine.

The question is then, Why does this happen on smaller files, and is there a workaround that allows the Binary read of smaller files.

DontVoteMeDown
  • 21,122
  • 10
  • 69
  • 105
Paul Shriner
  • 538
  • 3
  • 9

1 Answers1

23

Not sure why, but when you are using BinaryReader on an uploaded stream, the start position needs to be explicitly set.

b.BaseStream.Position = 0;
Konerak
  • 39,272
  • 12
  • 98
  • 118
Paul Shriner
  • 538
  • 3
  • 9