Link to home
Start Free TrialLog in
Avatar of beeker
beekerFlag for United States of America

asked on

Loading images into a BLOB field in MySQL 5.1.3

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

SOLUTION
Avatar of Tomas Helgi Johannsson
Tomas Helgi Johannsson
Flag of Iceland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of beeker

ASKER

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.

Avatar of beeker

ASKER

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

Avatar of beeker

ASKER

Image didn't link before.  Odd.  Let's try again.
Avatar of beeker

ASKER

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

http://ohshot.com/depot2/17f774.png
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of beeker

ASKER

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.
Avatar of beeker

ASKER

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