welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Monday, June 6, 2011

How to validate uploaded images in database using ASP.NET


In this article i am going to explain about How  to validate uploaded image in to database.For an every image contain  some unique Hexadecimal code,By comparing the code with upload image ,we can easily find out the image is real or not,Its one of the easiest technique i found.

For your refrence in previous article i explain about How to upload text file in database..

Follow the below process and code...

Before in SQL you have to create a table like this..
CREATE TABLE [dbo].[upload](
 [fileid] [int] IDENTITY(1,1) NOT NULL,
 [ImageName] [varchar](max) COLLATE SQL_Latin1_General_CP1_CS_AS NULL,
 [Image] [image] NULL
'client side coding

 If FileUpload1.HasFile Then

                Dim imageSize As Byte()
                Dim intlength As Integer

           Dim bytes1() As Byte = pstFile(FileUpload1) 'This fuction is used to Read Bytes from the upload File

            If filech(bytes1) = False Then  'This Function used to check upload file is image or Not
                    Label1.Text = "Not Real Image file"
                    Exit Sub
                End If

                ' Create SQL Connection 
                Dim con As New SqlConnection()
                con.ConnectionString = ConfigurationManager.ConnectionStrings("phone").ConnectionString

                intlength = Convert.ToInt32(FileUpload1.PostedFile.InputStream.Length)
                Dim cmd As New SqlCommand()
                cmd.CommandText = "INSERT INTO upload(ImageName,Image) VALUES (@ImageName,@Image)"
                cmd.CommandType = CommandType.Text
                cmd.Connection = con
                Dim ImageName As New SqlParameter("@ImageName", SqlDbType.VarChar, 50)
                ImageName.Value = TextBox1.Text.ToString

                Dim imagelenth As New SqlParameter("@Image", SqlDbType.Image)
                imagelenth.Value = bytes1
                Dim result As Integer = cmd.ExecuteNonQuery()


                Label1.Text = "please select the file to uplaod"
            End If

'This Function used to check upload file is image or Not........

 Function filech(ByVal byte1 As Byte()) As Boolean
        Dim fileheader As New Dictionary(Of String, Byte())
        fileheader.Add("JPG", New Byte() {&HFF, &HD8, &HFF, &HE0})
        fileheader.Add("JPEG", New Byte() {&HFF, &HD8, &HFF, &HE0})
        fileheader.Add("PNG", New Byte() {&H89, &H50, &H4E, &H47})
        fileheader.Add("TIF", New Byte() {&H49, &H49, &H2A, &H0})
        fileheader.Add("TIFF", New Byte() {&H49, &H49, &H2A, &H0})
        fileheader.Add("GIF", New Byte() {&H47, &H49, &H46, &H38})
        fileheader.Add("BMP", New Byte() {&H42, &H4D})
        fileheader.Add("ICO", New Byte() {&H0, &H0, &H1, &H0})

        Dim ext As String
        Dim tmp As Byte()
        Dim fheadr As Byte()

        If FileUpload1.HasFile Then
            ext = FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf(".") + 1).ToUpper
            tmp = fileheader(ext)
            fheadr = New Byte(tmp.Length - 1) {}

            For i As Integer = 0 To tmp.Length - 1
                If tmp(i) <> byte1(i) Then
                    Return False
                    Exit For
                    Return True
                End If
        End If
    End Function
'This fuction is used to Read Bytes from the upload File
Function pstFile(ByVal passfile As FileUpload) As Byte()
        Dim fs As Stream
        Dim bytes1 As Byte()
        Dim postfile As Byte()
        fs = passfile.PostedFile.InputStream
        Dim br1 As New BinaryReader(fs)
        bytes1 = br1.ReadBytes(passfile.FileContent.Length)
        postfile = bytes1
        Return postfile
    End Function

I hope this one really helpful to you........


Post a Comment