Reading stream twice?
Well, the simplest way is:
file.InputStream.Position = 0;
... assuming the stream supports seeking. However, That may do interesting things to the Image
if you're not careful - because it will have retained a reference to the stream.
You may be best off loading the data into a byte array, and then creating two separate MemoryStream
objects from it if you still need to. If you're using .NET 4, it's easy to copy one stream to another:
MemoryStream ms = new MemoryStream();
Request.Files["logo"].InputStream.CopyTo(ms);
byte[] data = ms.ToArray();
In addition to Jon's answer: If you have a StreamReader
, there is no Position parameter - instead you need to access its BaseStream
.
For this purpose you can use a little extension method ResetStreamReader
:
public static class Extension
{
public static void ResetStreamReader(this StreamReader sr, long position = 0)
{
if (sr == null) return;
sr.BaseStream.Position = position;
}
}
Put it inside your public static Extension class, then you can use it like so:
sr.ResetStreamReader();
Or you can pass the desired start position, if you want to start with a position > 0, for example if you want to skip headers when you read the stream again.