Re: Storing images in SQL DB example code

  •  10-17-2007, 8:24 AM

    Re: Storing images in SQL DB example code

    heres the code if anyone is interested.
     

    --On Page that holds the Editor--
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Editor1.Setting("CuteEditorFileStorageType") = GetType(SqlFileStorage).AssemblyQualifiedName
            Editor1.Setting("DownFile") = ResolveUrl("DownFile.Aspx")
            Editor1.SetSecurityGalleryPath("/")

    End Sub

    --Downfile.aspx codebehind--
    Imports System
    Imports System.Collections
    Imports System.ComponentModel
    Imports System.Data
    Imports System.Drawing
    Imports System.Web
    Imports System.Web.SessionState
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    Imports System.Web.UI.HtmlControls
    Imports System.Data.SqlClient
    Imports System.Configuration

    Partial Public Class DownFile
        Inherits System.Web.UI.Page

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        End Sub
        Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
            Dim dt As DateTime
            Dim filename As String
            Dim filesize As Integer
            Dim filedata As Byte()
            ' Using
            Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("myConnection").ConnectionString)
            Try
                conn.Open()
                Dim fileid As Integer = Integer.Parse(Request.QueryString("fileid"))
                ' Using
                Dim cmd As SqlCommand = New SqlCommand("select * from fsitems where id=@p0", conn)
                Try
                    cmd.Parameters.AddWithValue("@p0", fileid)
                    ' Using
                    Dim reader As SqlDataReader = cmd.ExecuteReader
                    Try
                        If Not reader.Read Then
                            Throw (New HttpException(404, " File Not Found "))
                        End If
                        dt = reader.GetDateTime(reader.GetOrdinal("createdt"))
                        filename = reader.GetString(reader.GetOrdinal("filename"))
                        filesize = reader.GetInt32(reader.GetOrdinal("filesize"))
                        filedata = CType(reader("filedata"), Byte())
                    Finally
                        CType(reader, IDisposable).Dispose()
                    End Try
                Finally
                    CType(cmd, IDisposable).Dispose()
                End Try
            Finally
                CType(conn, IDisposable).Dispose()
            End Try
            Response.ClearHeaders()
            Response.AddHeader("Content-Length", filesize.ToString)
            Response.BinaryWrite(filedata)
            Response.End()
        End Sub


    End Class

View Complete Thread