1

I have a file containing a column of numbers that I wish to read into a string. The file, however, is not being imported using a location on my hard disk, rather it's being uploaded via a FileUpload control.

I was wondering if there is a way I could read in the text from this file. I've looked into StreamReader, but that requires I have a string that's the path name of the file.

Is there another way around this?

Thanks :)

Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
Delfino
  • 967
  • 4
  • 21
  • 46

3 Answers3

3

A StreamReader requires a stream, not a path. Based on the documentation on MSDN (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.filecontent.aspx) you can use the FileContent member of the FileUpload control and feed that into a StreamReader.

Pete Garafano
  • 4,863
  • 1
  • 23
  • 40
2

Assuming you are working with ASP.NET (C# is just a programming language) the FileUpload control will save the file on the server in a temporary location that is made available to your session.

Then all you have to do is open the temporary file and read it's contents (using a StreamReader or something else)

Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
1

This is a duplicate of: Looping trough lines of txt file uploaded via FileUpload control

Which sugested this solution, to pass a FileUpload.FileContent to the StreamReader and looping through the contents:

{

    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();
                    }
                    }
} 
Community
  • 1
  • 1
Gustavo Azevedo
  • 113
  • 1
  • 7