2011-08-22 25 views
7

Tôi muốn chọn tệp .txt đơn giản chứa các dòng chuỗi bằng cách sử dụng điều khiển Tải lên tệp. Nhưng thay vì thực sự lưu các tập tin tôi muốn loop trough mỗi dòng văn bản và hiển thị mỗi dòng trong một ListBox kiểm soát.Lặp lại các đường rãnh của tệp txt được tải lên qua điều khiển FileUpload

Ví dụ về một tập tin văn bản:

test.txt

123jhg345
182bdh774
473ypo433
129iiu454

cách tốt nhất để thực hiện điều này là gì?

Những gì tôi có cho đến nay:

private void populateListBox() 
{ 
    FileUpload fu = FileUpload1; 

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

Trả lời

14
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(); 
    } 
} 
+0

Cảm ơn Shalini, Đây là những gì tôi đang tìm kiếm. Lưu ý rằng trình đọc luồng cần được khởi tạo như vậy: ** Trình đọc StreamReader = new StreamReader (fu.PostedFile.InputStream); ** – PercivalMcGullicuddy

+0

Cảm ơn bạn. bạn đã cứu ngày của tôi. –

4

mở tập tin vào một StreamReader và sử dụng


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

Nếu bạn muốn biết làm thế nào để có được những tập tin/ngày vào một dòng hãy nói trong những gì hình thức bạn nhận được các tập tin (s byte)

8

Dưới đây là một ví dụ làm việc:

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

Có một vài cách khác nhau để thực hiện việc này, những cách trên là ví dụ điển hình.

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(); 
} 
0

Có này là tốt, sử dụng HttpPostedFileBase trong MVC:

[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;// 
0
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.... 
     } 
    } 
} 
Các vấn đề liên quan