Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

Loading images into a BLOB field in MySQL 5.1.3

Avatar of beeker
beekerFlag for United States of America asked on
ASPMySQL Server
8 Comments1 Solution1365 ViewsLast Modified:
Okay I already know that loading images into a database is not recommended.  So don't start with me on that issue.  The task laid before me requires that I load the images (jpeg/png/gif) into a database.  I can't enumerate the why's but suffice it to say it's a must in this instance.  Also I have to do this in classic ASP.  PHP and .Net are not an option here.

Now... Below is the code to process all the images in a specific directory on the webserver.  This actually does work to loop through the files and creates the records.  However the BLOB data field is NULL.  I gotta be missing something but 2 solid days of researching this have not gotten me any further.

<!-- #include virtual="/inc/adovbs.inc" -->
<!-- #include virtual="/inc/CreateGUID.asp" --> 'simply returns a GUID and that's it.
<%
dim MyConn,fs,fldr,im,rs
const PNG = 0
const JPG = 1
const GIF = 2
dim binArray()

set MyConn = Server.CreateObject("ADODB.Connection")
MyConn.Open "Data Source=MyDSN;Database=MyDatabase"

set fs=Server.CreateObject("Scripting.FileSystemObject")
set fldr = fs.GetFolder(server.mappath("/uploads"))

for each i in fldr.files
    if instr(i.name,".jpg") or instr(i.name,".png") or instr(i.name,".gif") then
        response.write("Processing: "&i.path&"<br />")
        GUID = CreateGUID
        'use ADO.Stream to get the image data
        Set mystream = server.createobject("ADODB.Stream")
        mystream.Type = 1
        mystream.Open
        mystream.LoadFromFile i.path
        %><%=mystream.size%> bytes<br /><%
        %><br /><br /><%


        Set rs = CreateObject("adodb.recordset")
        rs.Open "photo_binaries", MyConn, 2, 3
        rs.AddNew
        rs.Fields("FileName") = i.name
        rs.Fields("FileSize") = i.size
        rs.Fields("FileID") = GUID
        rs.Fields("fileData").AppendChunk myStream.read
        rs.Update : rs.Close : set rs = nothing
        mystream.close : set mystream = nothing
    end if
next

MyConn.close : set MyConn = nothing
%>
And for the DBAs out there here's the SQL to create the table in MySQL 5.1.3 (generated via HeidiSQL).  
CREATE TABLE `photo_binaries` (
	`autoID` BIGINT(38) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'GUID',
	`FileID` VARCHAR(38) NOT NULL DEFAULT '' COMMENT 'GUID',
	`FileName` VARCHAR(255) NULL DEFAULT NULL COMMENT 'actual filen name',
	`FileSize` INT(11) UNSIGNED NULL DEFAULT NULL COMMENT 'bytes',
	`fileData` MEDIUMBLOB NULL COMMENT 'binary data for file',
	PRIMARY KEY (`autoID`)
)
ENGINE=InnoDB
ROW_FORMAT=DEFAULT
ASKER CERTIFIED SOLUTION
Avatar of beeker
beekerFlag of United States of America image

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Commented:
This problem has been solved!
Unlock 1 Answer and 8 Comments.
See Answers