2011-08-22 14 views
7

Chcę wybrać prosty plik .txt, który zawiera linie ciągów przy użyciu kontrolki FileUpload. Ale zamiast faktycznie zapisywać plik, chcę zapętlić przez każdą linię tekstu i wyświetlić każdą linię w kontrolce ListBox.Zapętlanie linii korytka pliku txt przesłanego za pomocą kontrolki FileUpload

Przykład pliku tekstowego:

test.txt

123jhg345
182bdh774
473ypo433
129iiu454

Jaki jest najlepszy sposób, aby osiągnąć ten cel?

Co mam tak daleko:

private void populateListBox() 
{ 
    FileUpload fu = FileUpload1; 

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

Odpowiedz

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

Dzięki Shalini, To jest to, czego szukasz. Zauważ, że czytnik strumienia musiał zostać zainicjowany jako taki: ** Czytnik StreamReader = nowy StreamReader (fu.PostedFile.InputStream); ** – PercivalMcGullicuddy

+0

Przyjacielu. uratowałeś mój dzień. –

4

otworzyć plik w StreamReader i używać


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

Jeśli chcesz wiedzieć, jak uzyskać plik/datę na strumień proszę powiedzieć, w jakiej formie pojawi się plik (s bajtów)

8

Oto przykład praca:

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

Istnieje kilka różnych sposobów robienia tego, powyższe są dobrymi przykładami.

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

Jest to także, używając HttpPostedFileBase w 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.... 
     } 
    } 
} 
Powiązane problemy