C# 中 UploadedFile 上带有 BinaryReader 的空数组
假设以下代码:
Stream file = files[0].InputStream;
var FileLen = files[0].ContentLength;
var b = new BinaryReader(file);
var bytes = b.ReadBytes(FileLen);
如果我上传一个包含 10 条记录(257 字节)的 CSV 文件,BinaryReader 会用“0”填充字节数组。
我还编写了一个循环来逐步执行 BinaryReader 的 ReadByte 方法,在循环的第一次迭代中,我收到以下异常:
无法读取超出流末尾
当我增加 CSV 时文件到20000条记录,一切正常。
那么问题是,为什么在较小的文件上会发生这种情况,是否有一种解决方法可以允许二进制读取较小的文件。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不知道为什么,但是当您在上传的流上使用 BinaryReader 时,需要显式设置起始位置。
Not sure why, but when you are using BinaryReader on an uploaded stream, the start position needs to be explicitly set.