Friday, September 12, 2008

Reading a text file stream asp.net

'You must first select a file with the browse button
'before you can run the code below.


Dim TempFile As String = System.IO.Path.GetTempFileName()
'Creates a uniquely named, zero-byte temporary file on
'disk and returns the full path of that file.
'This method creates a temporary file with a .TMP file extension.


File1.PostedFile.SaveAs(TempFile)
'File1 is my html input file object after using the browse
'button and selecting a file.


Dim TextSTR As String
TextSTR = My.Computer.FileSystem.ReadAllText(TempFile)
'My.Computer.FileSystem.ReadAllText returns the
'contents of a text file as a String


Response.Write(TextSTR)

System.IO.File.Delete(TempFile)
'Deletes the file

No comments: