ASP.Net Upload Images to Ms Access Memo Field

by top54u.com 24 Jul, 2008

MS Access memo field can also be used to upload images into the database table. You can create the binary stream of image in the memory and convert it into the base string to store the image into the MS Access memo field using ASP.Net VB code. In the same way you can retrieve the image from the memo field of MS Access database table and convert back it into binary stream to display it on ASP.Net web page. In the previous tutorials about image upload to MS Access database we used the OLE object type field. You can download the free code here to store and display the image to MS Access OLE Object type field.

 

ASP.Net VB Sample Code to Upload Image to MS Access Memo Field

 

In the ASP.Net web page you can use the file upload server control to upload the image. In the VB code behind you can use the PostedFile property of file upload control to get the Content type of image that returns the MIME type as image/jpeg, image/gif etc. PostedFile property of the file upload control also provides the InputStream property that returns image stream in the memory. Following code reads the InputStream of image and converts it into the Byte Array:

 

Dim imageSize As Int64

Dim imageType As String

Dim imageStream As Stream

 

' Gets the Size of the Image

imageSize = fileImgUpload.PostedFile.ContentLength

 

' Gets the Image Type

imageType = fileImgUpload.PostedFile.ContentType

 

' Reads the Image stream

imageStream = fileImgUpload.PostedFile.InputStream

Dim imageContent(imageSize) As Byte

Dim intStatus As Integer

intStatus = imageStream.Read(imageContent, 0, imageSize)

 

To pass the Byte Array of image stream to the MS Access memo field you can connect the ASP.Net web application to the Access Database using OLE DB provider. Before passing the Byte Array to memo field you have to convert the binary array to base 64 string as follows:

 

cmd.Parameters.Add("img_stream", OleDbType.LongVarWChar).Value = Convert.ToBase64String(imageContent)

 

In the above sample code you can see that OleDbType.LongVarWChar is used to pass the base64String that stores the data as System.String with Unicode null-termination.

Image retrieved from the MS Access memo field as base64String can be converted back to binary array stream to display it on the ASP.Net web page as follows:

 

Response.ContentType = dRow("img_type")

Response.BinaryWrite(Convert.FromBase64String(dRow("img_stream")))

 

Above code shows the Convert.FromBase64String method that converts the string to Byte Array.

Download the free source code for ASP.Net VB code to upload and display images to MS Access Memo field:

MSAccessImage.zip (102.73 kb)

Spotlight.....

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , , , , , , , ,

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

10/11/2008 3:29:58 AM




related videos.....
recent posts.....
top54u ezines.....