Click here to Skip to main content
15,905,563 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friend,
Currently i am facing a problem while reading filestream byte from
postedFile

foreach (HttpPostedFile postedFile in FileUpload1.PostedFiles)
            {
Stream fs = postedFile.InputStream;
                    BinaryReader br = new BinaryReader(fs);
                    Byte[] bytes = br.ReadBytes((Int32)fs.Length);

}
Exception coming as
ReadTimeout = 'fs.ReadTimeout' threw an exception of type 'System.InvalidOperationException'


Can anyone help me if i missing something in page level vlidation or else?

What I have tried:

Stream fs = postedFile.InputStream;
                    BinaryReader br = new BinaryReader(fs);
                    Byte[] bytes = br.ReadBytes((Int32)fs.Length);
Posted
Updated 24-Mar-17 1:19am

1 solution

Use the HttpPostedFile.ContentLength Property (System.Web)[^] instead of Stream.Length:
C#
Byte[] bytes = br.ReadBytes(postedFile.ContentLength);

[EDIT]
Or as shown in the example at HttpPostedFile.InputStream Property (System.Web)[^]:
byte[] bytes = new byte[postedFile.ContentLength];
fs.Read(bytes, 0, postedFile.ContentLength);
[/EDIT]
 
Share this answer
 
v2
Comments
Mukesh Ghosh 24-Mar-17 7:24am    
Not working.While save in SQLSERVER Table it store as <Binary data> for that.
Jochen Arndt 24-Mar-17 7:32am    
What is not working?
Error still there?
Or does it occur when writing somewhere?

I have updated my answer with code omitting the ByteReader.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900