C# Read from csv-file

I want to read a CSV file and put the data into an ArrayList. I use HtmlInputFile. It works fine when the user browse for the file and select the file. It works fine when I test this local on my machine.
But when I test it at the webserver, the filename refer to (for example) C:\myFile.csv but at the server and not the client.
Is it possible to get the file (stream) or filename in codebehind or do I have to use client script/JavaScript?
I want to read the csv file, validate that it is correct and then save the data into a database.
<INPUT id="inputFileName" type="file" runat="server">
---------------------
protected HtmlInputFile inputFileName;
 
private void btnValidateFile_Click(object sender, System.EventArgs e)
{
  if( inputFileName.PostedFile != null )
     LoadFromFile(inputFileName.PostedFile.FileName);
  else
  {
      // Output no fileName...
  }
}
 
protected void LoadFromFile(string _fileName)
{
  ArrayList RecordList = new ArrayList();
  string[] recordCollection;
  StreamReader cr = new StreamReader(_fileName)
  {
        while (cr.Peek() >=0)
	{
	  string tempString = cr.ReadLine();
	  recordCollection = tempString.Split(';');
	  RecordList.Add(recordCollection[0]);
	}
	cr.Close();
       .......
}

Open in new window

OskowichAsked:
Who is Participating?
 
OskowichConnect With a Mentor Author Commented:
ArrayList RecordList = new ArrayList();
if (inputFileName.PostedFile != null)
{
      string[] recordCollection;
      using (System.IO.StreamReader sr = new System.IO.StreamReader(inputFileName.PostedFile.InputStream))
      {
            while (sr.Peek() >=0)
            {
                  string tempString = sr.ReadLine();
                  recordCollection = tempString.Split(';');
                  RecordList.Add(recordCollection[0]);
            }
            sr.Close();
      }
}
...
0
 
OskowichAuthor Commented:
.NET 1.1
Visual Studio 2003
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.