Upload, Read, and Parse file in ASP.NET

While there are many tutorials for ASP.NET on uploading and saving a file to the disk as well as read and parsing a file from the disk, there is no simple tutorial for uploading, reading and parsing the data in memory. A client of ours needed to do this so I thought I’d list out the basics.

First you need a fileupload control and button on the ASPX page <asp:FileUpload ID="file1" runat="server" /> <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" />

Then code you button handler

(You’ll need to use/import the following libraries System.IO, System.Text)

C# // process file Stream theStream = file1.PostedFile.InputStream;

VB.NET ' process file Dim theStream As Stream = file1.PostedFile.InputStream

The code above simply creates a stream and sets it to the file you just uploaded.
Next we instantiate a stream reader and pass in the stream we just created

C# using (StreamReader sr = new StreamReader(theStream)) { …. }

VB.NET Using sr As New StreamReader(theStream) …. End Using

The … is just a placeholder for the code we’ll put in next. You can see that we’re just going to use the stream reader to do some stuff. We want to read each line in the file and place that line into a variable we can manipulate

C# string line; while ((line = sr.ReadLine()) != null) { …. }

VB.NET Dim line As String = sr.ReadLine() While Not line Is Nothing line = sr.ReadLine() ' CRITICAL line else you will go in an endless loop End While

Now, assuming our upload file is in the following format

ID|Name

Then we want to split the line and we’ll have our ID and Name for each line!

C# // split the line string[] tmpArray = line.Split(Convert.ToChar("|")); Response.Write("ID" + tmpArray[0].ToString() + " Name:" + tmpArray[1].ToString() + “<BR>");

VB.NET ' split the line Dim tmpArray() As String = line.Split(Convert.ToChar("|")) Response.Write("ID" & tmpArray(0).ToString() & " Name:" & tmpArray(1).ToString() & “<BR>")

There you have it. Now that you have your data, you can do whatever you like with it, throw it in the DB or dump it to a file.

The complete code looks like this
C# // process file Stream theStream = file1.PostedFile.InputStream; using (StreamReader sr = new StreamReader(theStream)) { string line; while ((line = sr.ReadLine()) != null) { // split the line string[] tmpArray = line.Split(Convert.ToChar("|")); Response.Write("ID" + tmpArray[0].ToString() + " Name:" + tmpArray[1].ToString() + “<BR>"); } }

VB.NET Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As EventArgs) ' process file Dim theStream As Stream = file1.PostedFile.InputStream Using sr As New StreamReader(theStream) Dim line As String = sr.ReadLine() While Not line Is Nothing 'split the line Dim tmpArray() As String = line.Split(Convert.ToChar("|")) Response.Write("ID" & tmpArray(0).ToString() & " Name:" & tmpArray(1).ToString() & “<BR>") line = sr.ReadLine() End While End Using End Sub

POSTED BY Divergence Hosting on Mar 12 under ASP.NET, C#, Tutorials, VB.NET

Leave a Comment

If you would like to make a comment, please fill out the form below.

Name (required)

Email (required)

Website

Comments

1 Comment so far
  1. envirodat May 23, 2010 7:41 pm

    Thank you for posting this. The code was simple, clean and easy to follow and allowed me to do what I needed to do. I appreciate you sharing this.

Copyright Divergence Hosting Technical Blog | Powered by WordPress | Using the GreenTech Theme

SEO Powered by Platinum SEO from Techblissonline