Export (0) Print
Expand All

HttpPostedFile.InputStream Property

Gets a Stream object that points to an uploaded file to prepare for reading the contents of the file.

Namespace: System.Web
Assembly: System.Web (in system.web.dll)

public Stream InputStream { get; }
/** @property */
public Stream get_InputStream ()

public function get InputStream () : Stream

Not applicable.

Property Value

A Stream pointing to a file.

The following code example shows how to read the contents of the first file in the client's file collection into a byte array, and then copy the byte array to a string.

using System;
using System.Web;
using System.Web.UI;

public class Page1: Page
 protected string MyString;
 private void Page_Load(Object sender, EventArgs e)
   HttpFileCollection MyFileCollection;
   HttpPostedFile MyFile;
   int FileLen;
   System.IO.Stream MyStream;
   MyFileCollection = Request.Files;
   MyFile = MyFileCollection[0];
   FileLen = MyFile.ContentLength;
   byte[] input = new byte[FileLen];
   // Initialize the stream.
   MyStream = MyFile.InputStream;
   // Read the file into the byte array.
   MyStream.Read(input, 0, FileLen);
   // Copy the byte array into a string.
   for (int Loop1 = 0; Loop1 < FileLen; Loop1++)
     MyString = MyString + input[Loop1].ToString();

import System.*;
import System.Web.*;
import System.Web.UI.*;

public class Page1 extends Page
    protected String myString;
    private void Page_Load(Object sender, EventArgs e)
        HttpFileCollection myFileCollection;
        HttpPostedFile myFile;
        int fileLen;
        System.IO.Stream myStream;

        myFileCollection = get_Request().get_Files();
        myFile = myFileCollection.get_Item(0);

        fileLen = myFile.get_ContentLength();
        ubyte input[] = new ubyte[fileLen];

        // Initialize the stream.
        myStream = myFile.get_InputStream();

        // Read the file into the byte array.
        myStream.Read(input, 0, fileLen);

        // Copy the byte array into a string.
        for (int loop1 = 0; loop1 < fileLen; loop1++) {
            myString = myString + input.get_Item(loop1).ToString();
    } //Page_Load
} //Page1

import System
import System.Web
import System.Web.UI

class Page1 extends Page{

 var myString : String

  function Page_Load(sender : Object, e : EventArgs){
    var myFileCollection : HttpFileCollection
    var myFile : HttpPostedFile
    var fileLen : int
    var myString : String = ""
    var myStream : System.IO.Stream

    myFileCollection = Request.Files
    myFile = myFileCollection[0]

    fileLen = myFile.ContentLength
    var input : Byte[] = new Byte[fileLen]

    // Initialize the Stream.
    myStream = myFile.InputStream

    // Read the file into the byte array.
    myStream.Read(input, 0, fileLen)

    // Copy the Byte array into a string.
    for(var i=0; i < fileLen; i++){
      myString = myString + input[i].ToString()


Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

© 2015 Microsoft