FileUpload to FileStream

Gopi picture Gopi · Jun 18, 2010 · Viewed 77.4k times · Source

I am in process of sending the file along with HttpWebRequest. My file will be from FileUpload UI. Here I need to convert the File Upload to filestream to send the stream along with HttpWebRequest. How do I convert the FileUpload to a filestream?

Answer

Gopi picture Gopi · Jun 21, 2010

Since FileUpload.PostedFile.InputStream gives me Stream, I used the following code to convert it to byte array

public static byte[] ReadFully(Stream input)
{
    byte[] buffer = new byte[input.Length];
    //byte[] buffer = new byte[16 * 1024];
    using (MemoryStream ms = new MemoryStream())
    {
        int read;
        while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            ms.Write(buffer, 0, read);
        }
        return ms.ToArray();
    }
}