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

Need help using the inputstream

3 Answers 390 Views
AsyncUpload
This is a migrated thread and some comments may be shown as answers.
John Crumpton
Top achievements
Rank 1
John Crumpton asked on 24 May 2010, 03:23 PM
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 

3 Answers, 1 is accepted

Sort by
0
Genady Sergeev
Telerik team
answered on 27 May 2010, 08:46 AM
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.
0
John Crumpton
Top achievements
Rank 1
answered on 08 Jun 2010, 07:35 PM

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 

0
Genady Sergeev
Telerik team
answered on 10 Jun 2010, 11:22 AM
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.
Tags
AsyncUpload
Asked by
John Crumpton
Top achievements
Rank 1
Answers by
Genady Sergeev
Telerik team
John Crumpton
Top achievements
Rank 1
Share this question
or