View comments | RSS feed
Contents > CFML Reference > ColdFusion Tags > cffile action = "readBinary" PreviousNext

cffile action = "readBinary"

Reads a binary file (such as an executable or image file) on the serve, into a binary object parameter that you can use in the page. To send it through a web protocol (such as HTTP or SMTP) or store it in a database, first convert it to Base64 using the ToBase64 function.

Note: This action reads the file into a variable in the local Variables scope. It is not intended for use with large files, such as logs, because they can bring down the server.

<cffile 
action = "readBinary"
file = "full_path_name"
variable = "var_name">

cfdirectory

Attribute

Req/Opt

Default

Description

action

Required

 

Type of file manipulation that the tag performs.

file

Required

 

Pathname of a binary fine to read.

If not an absolute path (starting a with a drive letter and a colon, or a forward or backward slash), it is relative to the ColdFusion temporary directory, which is returned by the GetTempDirectory function.

variable

Required

 

Name of variable to contain contents of binary file.

You convert the binary file to Base64 to transfer it to another site.

The following example creates a variable named aBinaryObj to contain the ColdFusion MX executable:

<cffile action = "readBinary" 
file = "c:\cfusion\bin\cfserver.exe"
variable = "aBinaryObj">

Contents > CFML Reference > ColdFusion Tags > cffile action = "readBinary" PreviousNext

ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6.1

Comments are no longer accepted for ColdFusion MX 6.1. ColdFusion 8 is the current version.

Comments


lbryngel said on Feb 18, 2004 at 8:28 AM :
SPELLING: file / Required / Pathname of a binary FINE to read.
CORRECTION: Pathname of a binary FILE to read.

We are working to improve the examples in the ColdFusion reference pages. We propose to replace the current example on this page with the the following example. If you have any comments on this example, add them to this page.

<< Old example was using cfserver.exe >>

<cffile action = "readBinary" file = "C:\Inetpub\wwwroot\cfdocs\getting_started\photos\somewhere.jpg" variable = "aBinaryObj">

<!--- Output binary object to jpg format for viewing --->
<cffile action="write" file = "c:\filesPdates\somewhereB.jpg" output="#toBinary(aBinaryObj)#">

<!--- HTML to view image --->
<img src="C:\filesPdates\somewhereB.jpg">
dnimmich said on Apr 3, 2004 at 9:57 AM :
If this tag (or action) is not intended for use with large files, which tag or function actually is intended for it? Or don't Coldfusion developers need to read or process "large" files?
jrunrandy said on Apr 8, 2004 at 6:00 AM :
The "large files" note is relative to the amount of available memory and the potential number of simultaneous users. If available memory is too small or there are too many simultaneous users, you might experience out of memory errors or increased response time due to disk swapping.
No screen name said on Jul 9, 2004 at 11:33 PM :
Qouting from above:

<cffile action = "readBinary" file = "C:\Inetpub\wwwroot\cfdocs\getting_started\photos\somewhere.jpg" variable = "aBinaryObj">

<!--- Output binary object to jpg format for viewing --->
<cffile action="write" file = "c:\filesPdates\somewhereB.jpg" output="#toBinary(aBinaryObj)#">

<!--- HTML to view image --->
<img src="C:\filesPdates\somewhereB.jpg">

*** End of Qoute

Does that work? Can the <img> tag reference an image outside of the document root. I guess if you setup coldfusion to reference to C:\files that would work.

What about reversing the example.
Copying a file from outside the document root into it. And then showing it with an <img> tag? Could that work.

Also, is there a way to use <cffile action="read" file="/outside document root/someImageFile.jpg" variable="request.image" /> and shove the data directly into an <img> tag, like <img src="#toBase64( request.image )#" />? Is there a way to do something like that?
halL said on Jul 12, 2004 at 7:00 AM :
LiveDocs is for documentation comments only and does not receive enough traffic to handle questions such as this. I suggest posting your issue to the online forums: http://webforums.macromedia.com/coldfusion/.
djdenv said on Jan 6, 2005 at 11:20 AM :
Hello... I successfully did what you're trying to do...
I'm posting all my code below... essentially it reads in a base64 string from an XML file, then pushes it out as the image:

<cftry>
<cffile action="read" file="#cgi.APPL_PHYSICAL_PATH#output.xml" variable="myXML">
<cfcatch type="any">
<cfoutput>#cfcatch.Message#</cfoutput>
</cfcatch>
</cftry>

<cfscript>
if (IsDefined("myXML"))
{
myXMLDoc = XmlParse(myXML);

selectedElements = XmlSearch(myxmldoc, "/ALERTS/*/Image/");

for (i = 1; i LTE ArrayLen(selectedElements); i = i + 1)
{
if (selectedElements[i].XmlAttributes["ID"] EQ #url.PhotoID#)
{
selectedElement =selectedElements[i];
data = ToBinary(SelectedElement.XmlAttributes["Data"]);
break;
}
}
}
</cfscript>

<cfcontent type="image/jpeg" reset="Yes"><cfoutput>#ToString(data)#</cfoutput>

Note, that the cfoutput and cfcontent MUST be on the same line. Otherwise, the carriage return is added to the data sent to the client and results in a broken image.
methik said on Feb 7, 2005 at 12:19 PM :
It took forever for me to figure out how to do this, so I thought I would pass my findings along. If you want to upload any type of file (binary) into your database with coldfusion, here's how I did it. Yes, I'm aware that it is better to store a path to the file you want to access in the database rather than the file itself, but in some cases, storing the file directly in the database is needed.

Here's my example:

<cffile
action="readBinary"
file="TestDoc.doc"
variable="TargetFile">

<cfquery name="InsertFile" datasource="myDatabase">
INSERT INTO FileStore
(
FileName,
FileData
)
VALUES
(
'myFile',
<cfqueryparam value="#TargetFile#" cfsqltype="cf_sql_blob">
)
</cfquery>

It was the <cfqueryparam> that was trickey for me - you cant just use the #TargetFile# reference as ColdFusion seems to try to convert it to a string? Additionally, I had to change the data type of the 'FileData' column in my database to an 'image' type, as 'binary' didn't seem to work.
Tacs said on Jul 20, 2005 at 11:22 AM :
jrunrandy still has not awnsered dnimmich's question which is my question as well:
If this tag (or action) is not intended for use with large files, which tag or function actually is intended for it? Or don't Coldfusion developers need to read or process "large" files?

If a webserver is limited by memory and diskswapping is being slowed down with this tag, what alternative tag do we use to bypass this problem. Example: one that sends information out in 8kb chunks so that only 8kb of memory is needed.

Does coldfusion have a function like this?
I need to read a large file (8gb log file) from a location on the hard drive that the webserver does not have acess to.
Tacs said on Jul 20, 2005 at 11:26 AM :
Since the messages don't allow editing or deleting, here is some more information to append to my post.

The documentation should be edited to include a length variable, which limits the amount of information read in, so that you can read very large files with very little memory. The following is an example:
http://us3.php.net/manual/en/function.fread.php
Chris Peters said on Oct 24, 2006 at 6:34 AM :
To answer djdenv's concern about carriage returns, know that you can use the <cfsetting> tag to avoid this unreadable way of writing code.

Also, without using <cfsetting>, all of the carriage returns after <cftry>, <cffile>, etc. will be sent to the browser as well.

You can rewrite his example like so:

<cfsetting enablecfoutputonly="true">

<cftry>
<cffile action="read" file="#cgi.APPL_PHYSICAL_PATH#output.xml" variable="myXML">
<cfcatch type="any">
<cfoutput>#cfcatch.Message#</cfoutput>
</cfcatch>
</cftry>

<cfscript>
if (IsDefined("myXML"))
{
myXMLDoc = XmlParse(myXML);

selectedElements = XmlSearch(myxmldoc, "/ALERTS/*/Image/");

for (i = 1; i LTE ArrayLen(selectedElements); i = i + 1)
{
if (selectedElements[i].XmlAttributes["ID"] EQ #url.PhotoID#)
{
selectedElement =selectedElements[i];
data = ToBinary(SelectedElement.XmlAttributes["Data"]);
break;
}
}
}
</cfscript>

<cfcontent type="image/jpeg" reset="Yes">

<cfoutput>#ToString(data)#</cfoutput>

<cfsetting enablecfoutputonly="false">

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/tags-p33.htm