Loading images into a BLOB field in MySQL 5.1.3

beeker
beeker used Ask the Experts™
on
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
%>

Open in new window

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

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Tomas Helgi JohannssonDatabase Administrator / Software Engineer
Commented:
                  Hi!

Take a look at this example in the MySQL manual
http://dev.mysql.com/tech-resources/articles/vb-blob-handling.html

What your code is lacking is the keywords adOpenStatic and  adLockOptimistic when you open
a connection to add a new record.

Regards,
    Tomas Helgi

Author

Commented:
Nope that wasn't it.  I changed the hard-coded numbers to the named variables adOpenStatic and adLockOptimistic and it still doesn't work (and I made sure to include adovbs.inc so that the named variables are defined).

The line rs.Open "photo_binaries", MyConn, 2, 3 should be rs.Open "photo_binaries", MyConn, 3, 3 and that's the same effect as typing rs.Open "photo_binaries", MyConn, adOpenStatic, adLockOptimistic.  Still it doesn't seem to work.

The problem seems to be that I'm getting nothing back from the ADODB.Stream object when assigning it to to the rs.fields("fileData") via .AppendChunk.  I've tried Flush, CopyTo and just about everything else I can think of.

Author

Commented:
BTW I've also tried upping the max_packet_allowed to 16MB (SET max_packet_allowed=16*1024*1024;).

I've also gotten rid of the .AppendChunk since I'm using the ADODB.Stream and don't need to append data chunks anymore (see code below).

Still nothing.  The r<%=mystream.size%> bytes<br /> shows that the stream is actually getting the files as the output matches the file sizes.  Attached is a screenshot of the data view in HeidiSQL.  The two test images were read and records added to the database.  But the dang fileData fields are NULL.
<!-- #include virtual="/inc/adovbs.inc" -->
<!-- #include virtual="/inc/CreateGUID.asp" -->
<%
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 "SELECT * FROM photo_binaries WHERE 1=0", MyConn, adOpenStatic, adLockOptimistic
        rs.AddNew
        rs.Fields("FileName") = i.name
        rs.Fields("FileSize") = i.size
        rs.Fields("FileID") = GUID
        rs.Fields("fileData") = myStream.read
        rs.Update : rs.Close : set rs = nothing
        mystream.close : set mystream = nothing
    end if
next

MyConn.close : set MyConn = nothing
%>

Open in new window

11/26 Forrester Webinar: Savings for Enterprise

How can your organization benefit from savings just by replacing your legacy backup solutions with Acronis' #CyberProtection? Join Forrester's Joe Branca and Ryan Davis from Acronis live as they explain how you can too.

Author

Commented:
Image didn't link before.  Odd.  Let's try again.

Author

Commented:
Okaaaaaayyyy.... images don't link anymore?  Well here's the direct link to the screenshot.

http://ohshot.com/depot2/17f774.png
Commented:
Okay I think I'm making headway finally.  Modified a couple of the the script's lines to:

set MyConn = Server.CreateObject("ADODB.Connection")
MyConn.CursorLocation = adUseClient
MyConn.Open "Data Source=MyDSN;Database=MyDatabase;OPTION=" & 1 + 2 + 8 + 32 + 2048 + 16384


So now my fields aren't NULL anymore.  Still have to test writing them back out though.

Author

Commented:
Well now I'm in a quandry.....

I was able to get this to work.  The proposed solution from TomasHelgi wasn't correct but it did point me in a helpful direction where I was able to do some more searching and figured it out on my own from there.

Author

Commented:
The actual solution given was not correct.  However it did have a link to an article that did point me in the right direction.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial