Simple way to get text from uploaded file?

Thread is closed for posting
4 posts, 0 answers
  1. Aron
    Aron avatar
    80 posts
    Member since:
    Dec 2005

    Posted 15 Aug 2009 Link to this post

    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?
     
                     
                } 
     
            } 
  2. Aron
    Aron avatar
    80 posts
    Member since:
    Dec 2005

    Posted 15 Aug 2009 Link to this post

    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 
                } 
            } 
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Shane Woodruff
    Shane Woodruff avatar
    9 posts
    Member since:
    Mar 2010

    Posted 26 Apr 2010 Link to this post

    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 
  5. Genady Sergeev
    Admin
    Genady Sergeev avatar
    1596 posts

    Posted 29 Apr 2010 Link to this post

    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.
Back to Top