Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with tobase64

Status
Not open for further replies.

jeepxo

Programmer
Oct 1, 2002
228
CA
I'm inserting some files into a database.

I use the CFFILE tag to upload them to a temp location, then I use <CFFILE Action=&quot;binaryread&quot;> to read it in as a variable. I then use the ToBase64 function and insert it into a database table.

On the admin side of the site I read the file from the database then use the tobinary function and write it to a temp location for downloading.


If the file is less then 20 K this works flawlessly.
On files that are greater then 20 K the ToBase64 seems to fail.
I get this error on the admin side
Parameter 1 of funtion toBinary which is now blahblahblah must be a base64-encoded string.

Is there a size limit that the ToBase64 function has?
and if so how do I get around this?
 
I know the exact process that is taking place, but I'd need to see your code to help you troubleshoot it. I was messing with this exact thing the other day. While I have no use for it at all, it was still interesting to get it to work the right way.

-Tek
 
The code is very simple as I stated above.
On the upload side

<cffile action=&quot;readbinary&quot; file=#fileLocation# variable=&quot;myFile&quot;>
<cfset myFileForInsert = tobase64(#myFile#)>

<cfquery .......>
Insert into
</cfquery>

Then on the downlown side
<cfquery name=&quot;getFromDB&quot; ....>
<cfset dataForWriting=tobinary(#data_table#)>
</cfquery>

<cffile action=&quot;write&quot; file='#fileToWrite#' output=#dataForWriting#>


Where I fail is on the tobinary(#data_table#) if the original file was over 20 K. If the original file is smaller then 20K I have no problem.
 
Try using <cfqueryparam> on the insert.

Here is the code I tested with a 46kb image file; it works fine on my end. Note the datatype for the Image_Data field is TEXT.

Code:
<!--- image.cfm --->

<cffile action=&quot;READBINARY&quot; file=&quot;c:\picture.jpg&quot; variable=&quot;Img&quot;>

<cfset ImgObj = ToBase64(Img)>

<cfquery datasource=&quot;for_testing&quot; name=&quot;InsertPic&quot;>
INSERT INTO
	Images
	
	(Image_Data)
	
	VALUES
	
	(<cfqueryparam cfsqltype=&quot;CF_SQL_LONGVARCHAR&quot; value=&quot;#ImgObj#&quot;>)
</cfquery>

<!--- image_serve.cfm --->

<cfquery datasource=&quot;for_testing&quot; name=&quot;Img&quot;>
SELECT Image_Data FROM Images
</cfquery>

<cfset PicData = ToBinary(Img.Image_Data)>

<cffile action=&quot;WRITE&quot; file=&quot;c:\picture2.jpg&quot; output=&quot;#PicData#&quot; addnewline=&quot;no&quot;>

<cfsetting enablecfoutputonly=&quot;No&quot;><CFHEADER NAME=&quot;Content-Disposition&quot; VALUE=&quot;inline; filename=picture2.jpg&quot;><cfcontent type=&quot;image/jpg&quot; file=&quot;c:\picture2.jpg&quot; deletefile=&quot;yes&quot;>

-Tek
 
I've tried the long_varchar type and the SQL_CLOB data types but I still have the same problem.

I've tried ntext, image and varbinary datatypes for the database.

What I suspect is happening is the file is being truncated at some point
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top