【问题标题】:Looping through lines of txt file uploaded via FileUpload control循环通过 FileUpload 控件上传的 txt 文件行
【发布时间】:2011-11-01 06:35:42
【问题描述】:

我想使用 FileUpload 控件选择一个包含字符串行的简单 .txt 文件。但我不想实际保存文件,而是要遍历每一行文本并在 ListBox 控件中显示每一行。

文本文件示例:

test.txt

123jhg345
182bdh774
473ypo433
129iiu454

最好的方法是什么?

到目前为止我所拥有的:

private void populateListBox()
{
  FileUpload fu = FileUpload1;

  if (fu.HasFile)
  {
    //Loop trough txt file and add lines to ListBox1
   }
 }

【问题讨论】:

    标签: c# asp.net file-upload upload filestream


    【解决方案1】:
    private void populateListBox() 
    {
        FileUpload fu = FileUpload1; 
        if (fu.HasFile)  
        {
            StreamReader reader = new StreamReader(fu.FileContent);
            do
            {
                string textLine = reader.ReadLine();
    
                // do your coding 
                //Loop trough txt file and add lines to ListBox1  
    
            } while (reader.Peek() != -1);
            reader.Close();
        }
    }
    

    【讨论】:

    • 感谢 Shalini,这就是我想要的。请注意,流阅读器需要这样初始化:StreamReader reader = new StreamReader(fu.PostedFile.InputStream);
    【解决方案2】:

    这是一个工作示例:

    using (var file = new System.IO.StreamReader("c:\\test.txt"))
    {
        string line;
        while ((line = file.ReadLine()) != null)
        {
            // do something awesome
        }
    }
    

    【讨论】:

      【解决方案3】:

      将文件打开到StreamReader 并使用

      
      while(!reader.EndOfStream) 
      { 
         reader.ReadLine; 
         // do your stuff 
      }
      

      如果您想知道如何将文件/日期放入流中,请说明您以什么形式获得文件(s 字节)

      【讨论】:

        【解决方案4】:

        有几种不同的方法可以做到这一点,以上是很好的例子。

        string line;
        string filePath = "c:\\test.txt";
        
        if (File.Exists(filePath))
        {
           // Read the file and display it line by line.
           StreamReader file = new StreamReader(filePath);
           while ((line = file.ReadLine()) != null)
           {
             listBox1.Add(line);
           }
             file.Close();
        }
        

        【讨论】:

          【解决方案5】:

          还有这个,在 MVC 中使用 HttpPostedFileBase:

          [HttpPost]
          public ActionResult UploadFile(HttpPostedFileBase file)
          {    
              if (file != null && file.ContentLength > 0)
              {
                    //var fileName = Path.GetFileName(file.FileName);
                    //var path = Path.Combine(directory.ToString(), fileName);
                    //file.SaveAs(path);
                    var streamfile = new StreamReader(file.InputStream);
                    var streamline = string.Empty;
                    var counter = 1;
                    var createddate = DateTime.Now;
                    try
                    {
                         while ((streamline = streamfile.ReadLine()) != null)
                         {
                              //do whatever;//
          

          【讨论】:

            【解决方案6】:
            private void populateListBox()
            {            
                List<string> tempListRecords = new List<string>();
            
                if (!FileUpload1.HasFile)
                {
                    return;
                }
                using (StreamReader tempReader = new StreamReader(FileUpload1.FileContent))
                {
                    string tempLine = string.Empty;
                    while ((tempLine = tempReader.ReadLine()) != null)
                    {
                        // GET - line
                        tempListRecords.Add(tempLine);
                        // or do your coding.... 
                    }
                }
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-11-17
              • 2013-02-15
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多