ASP example of saving an image to a database using base64

For many years we have shown examples of saving files, especially images, into a database field after uploading with our csASPUpload component or resizing with csImageFile. Recently we have added the functionality to export from both components using Base64 encoding. This allows an alternative method of saving to a database because a Base64 encoded file is text. It can be used in an INSERT INTO statement in SQL and saved in a large text field such as a Memo in Access or a LongText field in MySQL.

The csImageFile component has the commands Base64Out and Base64In for working with Base64 encoded images. The following example loads an image and saves it into a MySQL database, although changing the connection string would make it work with most database systems.

' The variables ServerName, Database and Password must be set first

DataConnection = "DRIVER={MySQL ODBC 5.3 Unicode Driver}; SERVER=" &_
    ServerName & "; DATABASE=" & Database & "; UID=" &_
    User & ";PASSWORD=" & Password & "; OPTION=3"

Set DBConn = Server.CreateObject("ADODB.Connection")

Set Image = Server.CreateObject("csImageFile64.Manage")
Path = "C:\images\"
FileName = "10000.jpg"
Image.ReadFile Path & FileName

SQL = "INSERT INTO Images(Name, ImageBase64) VALUES ('" & Filename & "', '" &_
    Image.Base64Out("jpg") & "')"

DBConn.Mode = 3 'Read/Write
DBConn.Open DataConnection
DBConn.Execute SQL

The above code assumes the database has two fields, Name and ImageBase64 which are a text field to hold the name of the file and a long text field for the image data.

The following code would display the image. The variable DataConnection contains the connection string.

Set DBConn = Server.CreateObject("ADODB.Connection")
DBConn.Open DataConnection

SQL = "SELECT ImageBase64 FROM Images WHERE Name='10000.jpg'"

Set Recordset = Server.CreateObject("ADODB.Recordset")
Recordset.Open SQL, DBConn
If not Recordset.EOF Then
%>
<img src="data:image/jpeg;base64,<%= Recordset("ImageBase64") %>" />
<%
End If
Recordset.Close
DBConn.Close

If different format images are to be stored, the format would have to be another database filed. This code assumes the image is a JPG. It is important to recognise that csImageFile does not include the content type of the data when it exports Base64. When the image is displayed, this content type must be included.

Base64 encoded data is 33% longer than the equivalent raw binary data. This is a disadvantage to storing data in this way.

From version 3.1 our csASPUpload component can also export data as a Base64 string. The FileData command used for binary output has been supplemented with the FileAsBase64 command. See the product documentation for full details.

There are some working examples in our live demonstration area.

Full list of available ASP demos and tutorials.

Cookies

This site uses cookies for functionality, traffic analysis and for targeted advertising. Click the Accept button to accept our Cookie Policy. The Cookie Policy page offers configuration for a reduced set of cookies for this site.