Prashant Pimpale
Prashant Pimpale

Reputation: 10697

How to read text line by line in HttpPostedFile

I am developing an API where the file is received a file in HttpPostedFile so I want to read the lines and iterate over the all lines:

public IList<string> ReadTextFileAndReturnData(HttpPostedFile file)
{
   IList<string> _responseList = new List<string>();

   //string result = new StreamReader(file.InputStream).ReadToEnd();
   // Not sure how to get all lines from Stream
   foreach (var line in lines)
   {
      // This is what I want to do
      // IList<string> values = line.Split('\t');
      // string data = values[0];
      // _responseList.Add(data);

   }
    return _responseList;
}

Upvotes: 0

Views: 2012

Answers (1)

Antoine V
Antoine V

Reputation: 7204

var lines = new List<string>();
using(StreamReader reader = new StreamReader(file.InputStream))
{
    do
    {
        string textLine = reader.ReadLine();
        lines.Add(textLine);  
    } while (reader.Peek() != -1);
}

Upvotes: 2

Related Questions