Need help using the inputstream

4 posts, 0 answers
  1. John Crumpton
    John Crumpton avatar
    12 posts
    Member since:
    Jul 2009

    Posted 24 May 2010 Link to this post

    I'm trying to use the AsyncUpload (also tried RadUpload w/ the same results) to upload image files and save them in an image field in SQL (not save the file to the filesystem) and then use RadBinaryImage to display them. I get the uploadedfiles collection and loop through it. If I use the saveas method, it works fine. If I use the inputstream (to do anything), I don't get the results I expected... It does write data to the SQL field, but it's not usable... either by RadBinaryImage, or streaming it back to a file... will not open. I also tried skipping the SQL field (for testing) and writing the inputstream directly to the filesystem and the resulting file will not open either. Code for this is below (it's mostly pilfered from Telerik examples...), the commented portion is skipping the DB and writing to file.

    If possible, could you post an example using the inputstream (or whatever method is best) to save an image to SQL and then display it using RadBinaryImage.

    Thanks
        Private Sub RadAsyncUpload1_FileUploaded(ByVal sender As ObjectByVal e As Telerik.Web.UI.FileUploadedEventArgs) Handles RadAsyncUpload1.FileUploaded  
     
            objCon = New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("HDSWebConnectionString").ConnectionString)  
            objCon.Open()  
            For Each file As UploadedFile In RadUpload1.UploadedFiles  
                Dim bytes(file.InputStream.Length - 1) As Byte 
                file.InputStream.Read(bytes, 0, file.InputStream.Length)  
     
                Try 
                    Dim command As New SqlCommand("insert into Images (Description,ImageData,ImagePath,Caption ) " & _  
                            "values ('','@Content','',''); SELECT @@IDENTITY", objCon)  
                    command.Parameters.AddWithValue("@Content", bytes)  
                    command.ExecuteNonQuery()  
                Finally 
     
                End Try 
            Next 
     
            'objCon = New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("HDSWebConnectionString").ConnectionString)  
            'objCon.Open()  
            'For Each file As UploadedFile In RadAsyncUpload1.UploadedFiles  
            '    Dim bytes(file.InputStream.Length - 1) As Byte  
            '    file.InputStream.Read(bytes, 0, file.InputStream.Length)  
     
            '    Try  
            '        Dim strfn As String = "C:\Users\jcrumpton\Documents\Test.jpg"  
            '        Dim fs As New FileStream(strfn, FileMode.CreateNew, FileAccess.Write)  
            '        fs.Write(bytes, 0, file.InputStream.Length)  
            '        fs.Flush()  
            '        fs.Close()  
     
            '    Finally  
     
            '    End Try  
            'Next  
     
     
        End Sub 
  2. Genady Sergeev
    Admin
    Genady Sergeev avatar
    1596 posts

    Posted 27 May 2010 Link to this post

    Hello John Crumpton,

    You need to use the Using statement when working with streams. More information regarding Using and streams can be found here. The important part here is that when you open the stream you need to close it after finished working with it. The Using statement automatically does this for you. In the end, your code must look like this:

    Using objCon As SqlConnection = New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("HDSWebConnectionString").ConnectionString)
              objCon.Open()
              Using stream As Stream = e.File.InputStream
                  'Do something with the stream
     
              End Using
     
          End Using



    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. John Crumpton
    John Crumpton avatar
    12 posts
    Member since:
    Jul 2009

    Posted 08 Jun 2010 Link to this post

    Even with the using block (or manually doing a .flush and .close), I still do not get the expected results...

       Private Sub RadUpload1_FileUploaded(ByVal sender As ObjectByVal e As Telerik.Web.UI.FileUploadedEventArgs) Handles RadUpload1.FileUploaded  
     
         
            
            Try 
                objCon = New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("HDSWebConnectionString").ConnectionString)  
                objCon.Open()  
     
                Dim imageData As Byte()  
     
                Using s As Stream = e.File.InputStream  
                    imageData = New Byte(s.Length) {}  
                    s.Read(imageData, 0, s.Length)  
                End Using  
             
                'e.File.InputStream.Flush()  
                'e.File.InputStream.Close()  
     
                Dim command As New SqlCommand("insert into Images (Description,ImageData,ImagePath,Caption ) " & _  
                        "values ('','@Content','',''); SELECT @@IDENTITY", objCon)  
                command.Parameters.AddWithValue("@Content", imageData)  
                command.ExecuteNonQuery()  
            Finally 
     
            End Try 
        End Sub 

  5. Genady Sergeev
    Admin
    Genady Sergeev avatar
    1596 posts

    Posted 10 Jun 2010 Link to this post

    Hello John,

    As an attachment you can find sample project that demonstrates how to save an image to a database and then successfully stream it to the client.

    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