I am attempting to parse an HTTP POST (from a NetworkStream) that is multipart/form-data with a file input field.

The problem is that I need to parse the stream with a StreamReader in order to parse the header/boundary strings, but the file itself is raw binary data in the middle of this string. I cannot figure out how to mix StreamReader and raw memory access to write this file out to the disk, because the ASCII encoding/decoding is trashing information in the binary data.

I need a way to mix text parsing with raw memory access. My best attempt is to copy the POST to a MemoryStream, use the stream reader, and remember the stream position when the boundarys are found. However, the StreamReader reads in 1024 byte increments, so this is not useful. I have not found a way to find the precise offset within the buffer that StreamReader is located.

Any ideas on how to parse this data?

I am using .NET 3.0 with Visual C# 2008.