Tuesday, August 4, 2009

Method to Insert images into blob field of oracle database tables !

First create a table with bolb field
example :
create table image_list
(
image_id NUMBER(5) not null,
PHOTO BLOB
)

then we want to upload the image that we want to store into the table image_list,for that iam now using Asp.net with vb coding for image file uploading

examples:

If Me.FileUpload1.HasFile Then
Dim fileExtension As String
fileExtension = System.IO.Path. _
GetExtension(Me.FileUpload1.FileName).ToLower()
Dim allowedExtensions As String() = _
{".jpg", ".jpeg"}
Dim fileok As Boolean
fileok = False
For i As Integer = 0 To allowedExtensions.Length - 1
If fileExtension = allowedExtensions(i) Then
fileok = True
End If
Next
If Not (fileok) Then
Dim cl_script As New StringBuilder
cl_script.Append(" alert('The File Type of Photo Not Supported!!--attach .jpg/.jpeg') ;")
Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "inv", cl_script.ToString, True)
Exit Sub
End If
End If
' This is the code to check the uploaded file type
'Now create an folder 'IMAGE' in your project ,so that we can store image temporarly and can dispose after use.then use the code below to attach the file

Dim DirPath As String
DirPath = Me.Server.MapPath("IMAGE")
Dim fnm, fnm1 As String
Try
If Me.FileUpload1.FileName <> "" Then
fnm = GetUniqueFilename(DirPath + "/empphoto.jpg")
If Me.FileUpload1.HasFile Then
Me.FileUpload1.SaveAs(fnm)
End If
Dim fs As New IO.FileStream(fnm, IO.FileMode.Open, IO.FileAccess.Read)
Dim bl(fs.Length) As Byte
fs.Read(bl, 0, fs.Length)
fs.Close()
image1 = bl
Dim fp As New IO.FileInfo(fnm)
If fp.Exists Then
fp.Delete()
End If
int1 = 1
End If
'This is the code to insert the image into the table.
dim sql as string = "insert into image_list(image_id, PHOTO) values(:id,:photo)"

parm_coll1(0) = New OracleParameter
parm_coll1(0).ParameterName = "id"
parm_coll1(0).OracleType = OracleType.Number
parm_coll1(0).Direction = ParameterDirection.Input
parm_coll1(0).Value = 1
' you can increment the id, if you wish
parm_coll1(1) = New OracleParameter
parm_coll1(1).ParameterName = "photo"
parm_coll1(1).OracleType = OracleType.Blob
parm_coll1(1).Direction = ParameterDirection.Input
parm_coll1(1).Value = image2
oh.ExecuteNonQuery(sql, parm_coll1)

Catch ex As Exception
Response.Write(ex.Message.ToString)
End Try

Bilumon V B
email- bilumon@gmail.com

No comments:

Post a Comment