This is a migrated thread and some comments may be shown as answers.

Simple way to get text from uploaded file?

3 Answers 512 Views
Upload (Obsolete)
This is a migrated thread and some comments may be shown as answers.
Aron
Top achievements
Rank 2
Aron asked on 15 Aug 2009, 04:38 PM
Hello,
What's the easiest way to get the text of an uploaded file stream? Note I do not plan to save the actual file. Just get it's text and save that.

// save 
        if (RadUploadSQL.UploadedFiles.Count>0) 
        { 
            foreach (UploadedFile validFile in RadUploadSQL.UploadedFiles) 
            { 
                // open sql file 
 
                byte[] Filedata; 
                int Filesize; 
                Filesize = Convert.ToInt32(validFile.InputStream.Length); 
                validFile.InputStream.Read(Filedata, 0, Filesize);
// What to do here?
 
                 
            } 
 
        } 

3 Answers, 1 is accepted

Sort by
0
Aron
Top achievements
Rank 2
answered on 15 Aug 2009, 06:46 PM
Hello,
Solved it
Here's the solution in case anyone else is looking:

if (RadUploadSQL.UploadedFiles.Count > 0) 
        { 
            foreach (UploadedFile validFile in RadUploadSQL.UploadedFiles) 
            { 
                // open the text file 
                using (StreamReader reader = new StreamReader(validFile.InputStream)) 
                    v.SqlScript = reader.ReadToEnd(); // set value to text files text 
            } 
        } 
0
Shane Woodruff
Top achievements
Rank 1
answered on 27 Apr 2010, 12:23 AM
I'm having a similar problem. I'd like to upload a single text file to the server, and parse its contents. I can see the length is correct in theStream, so I know it has content before it hits the reader.

However when I try to use the StreamReader, it comes back with Nothing. What am I missing?

Here is my code:
Imports System.IO  
 
 Protected Sub buttonSubmit_Click(ByVal sender As ObjectByVal e As System.EventArgs) Handles buttonSubmit.Click  
    BindValidResults()  
    BindInvalidResults()  
 
    ' process file  
    Dim theStream As Stream = RadUpload1.UploadedFiles(0).InputStream  
 
    Using sr As New StreamReader(theStream)  
      Dim line As String = sr.ReadLine()  
      While Not line Is Nothing 
        'split the line  
        Dim tmpArray() As String = line.Split(Convert.ToChar("|"))  
        Response.Write("ID" & tmpArray(0).ToString() & " Name:" & tmpArray(1).ToString() & "<BR>")  
        line = sr.ReadLine()  
      End While 
    End Using  
 
  End Sub 
0
Genady Sergeev
Telerik team
answered on 29 Apr 2010, 11:30 AM
Hi Shane Woodruff,

I have tested your code on a sample page and it seems to work fine. I suggest that you put a breakpoint into your code and debug it see what exactly is going wrong. Is this issue reproduced only with a specific file or with all kinds of files? I am attaching my sample project that works fine. Can you reproduce the issue on it?

Kind regards,
Genady Sergeev
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Upload (Obsolete)
Asked by
Aron
Top achievements
Rank 2
Answers by
Aron
Top achievements
Rank 2
Shane Woodruff
Top achievements
Rank 1
Genady Sergeev
Telerik team
Share this question
or