如何在HttpPostedFile中逐行读取文本

时间:2018-10-03 12:15:54

标签: c#

我正在开发一个API,在其中接收文件的是HttpPostedFile中的文件,因此我想读取所有行并遍历所有行:

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;
}

1 个答案:

答案 0 :(得分:1)

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