Posting File Data Using A Base64 Encoding In ColdFusion

Posted January 4, 2008 at 10:35 AM

Tags: ColdFusion, Ask Ben

This was sort of based on an Ask Ben inquiry, so I will include it in this section, but it's not entirely accurate. Someone had asked me about passing a file to a web service and I had suggested that one of the ways to do this was to pass it using a Base64 encoding of the binary data. This would allow the data to be easily defined using XML, which is how many web services function (think SOAP, think XML-RPC). While I am not following SOAP or XML-RPC standards in this quick demo, the ideas stay the same.

There are two players in this game. One entity is the file that reads the file data, encodes it, and submits it to the web service. The other entity is the actual web service file that accepts the data, converts the encoded data back to binary, and writes it to a file.

To demo this, we are going to first read in a JPG file and then post it as XML data using the ColdFusion's CFHttp and CFHttpParam tags:

 Launch code in new window » Download code as text file »

  • <!---
  • Create the path to the binary file that we want to
  • post via a web service.
  • --->
  • <cfset strFilePath = ExpandPath( "./girls_in_bikinis.jpg" ) />
  •  
  •  
  • <!--- Read in the binary file. --->
  • <cffile
  • action="readbinary"
  • file="#strFilePath#"
  • variable="objBinaryData"
  • />
  •  
  •  
  • <!---
  • Create the XML that we are going to post. When posting this
  • file, we are going to encode it as base64 text data.
  • --->
  • <cfsavecontent variable="strXmlData">
  • <cfoutput>
  •  
  • <file>
  • <name>#XmlFormat(
  • ListLast( strFilePath, "\/" )
  • )#</name>
  •  
  • <ext>#XmlFormat(
  • ListLast( strFilePath, "." )
  • )#</ext>
  •  
  • <!---
  • When storing the binary data, we are going to
  • pass the file as a Base64 encoding. I am like
  • 95% sure that this will result in only valid
  • XML characters, but I am not completely sure.
  • --->
  • <base64>#ToBase64(
  • objBinaryData
  • )#</base64>
  • </file>
  •  
  • </cfoutput>
  • </cfsavecontent>
  •  
  •  
  • <!---
  • Build the URL for the "web service" to which we are going
  • to post this file data.
  • --->
  • <cfset strUrl = (
  • GetDirectoryFromPath(
  • GetPageContext().GetRequest().GetRequestUrl().ToString()
  • ) &
  • "webservice.cfm"
  • ) />
  •  
  •  
  • <!--- Post the XML data to the "web service" file. --->
  • <cfhttp
  • url="#strUrl#"
  • method="post">
  •  
  • <!--- Post XML data. --->
  • <cfhttpparam
  • type="xml"
  • value="#strXmlData#"
  • />
  •  
  • </cfhttp>

As you can see, when I store the data in the XML string, I am being very careful not to include anything that is NOT XML safe. I am, however, not encoding the Base64 data. That makes me nervous, but I feel like 95% sure that Base64 is inherently XML safe since the Base64 data type is part of the XML-RPC standard as well as the XStandard web services SOAP definition. Once we have our XML string, we are simply posting it to the target URL using the CFHttpParam tag of type XML. Using this tag, the CFHttp request automatically sets the content to be of type text/xml and defines our XML string as the request data content.

Now, on the other side of the submission, we have our web service, which takes the XML post, extracts the Base64 data, converts it back to Binary, and writes it to the file system:

 Launch code in new window » Download code as text file »

  • <!--- Grab the content from the request data post. --->
  • <cfset strContent = Trim( GetHttpRequestData().Content ) />
  •  
  • <!--- Check to see if the content is a valid XML document. --->
  • <cfif IsXml( strContent )>
  •  
  • <!---
  • Parse the XML data into a ColdFusion Xml
  • Document Object.
  • --->
  • <cfset xmlPost = XmlParse( strContent ) />
  •  
  • <!---
  • Check to see if we have all the XML nodes that we
  • need in order to save the file. For our naming
  • purposes, all we need is the file extension and
  • the base64 data.
  • --->
  • <cfif (
  • StructKeyExists( xmlPost.file, "ext" ) AND
  • StructKeyExists( xmlPost.file, "base64" )
  • )>
  •  
  • <!---
  • ASSERT: We have the nodes that we need, and for
  • this demo, we are just going to assume that the
  • data values are valid.
  • --->
  •  
  • <!---
  • Create the file name for the target file. We are
  • going to use the passed in extension and a UUID.
  • Make sure that file name points to the executing
  • script directory (ExpandPath()).
  • --->
  • <cfset strFileName = ExpandPath(
  • CreateUUID() & "." &
  • xmlPost.file.ext.XmlText
  • ) />
  •  
  • <!---
  • Grab the base64 file data and convert it to
  • binary data.
  • --->
  • <cfset objBinaryData = ToBinary(
  • xmlPost.file.base64.XmlText
  • ) />
  •  
  •  
  • <!--- Write the binary file data to disk. --->
  • <cffile
  • action="write"
  • file="#strFileName#"
  • output="#objBinaryData#"
  • />
  •  
  • </cfif>
  •  
  • </cfif>

I have not included any real error handling or fail handlers as this is just a demo in a controlled environment. If this were a real web service, I am sure you'd want to have CFTry / CFCatch blocks as well as a return value to indicate success or error messages. A lot of that depends on the type of standards you are using (ex. XML-RPC has a very defined way to return errors).

There's not a whole lot going on here. Really, it's just about knowing how to use and leverage ColdFusion's ToBase64() and ToBinary() methods. If you want to see any more of this, my ColdFusion XStandard Web Services project provides file uploading using a SOAP version of this idea.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Jan 4, 2008 at 11:20 AM // reply »
7 Comments

As always Ben you don't disappoint. Thanks for the info on these functions.


Jan 4, 2008 at 11:23 AM // reply »
6,516 Comments

@Paul,

My pleasure! Let me know if you have any follow-up questions.


Jan 4, 2008 at 11:25 AM // reply »
5 Comments

As a suggestion, you may want to wrap the base64 encoded data in a CDATA block since it's being transmitted in an XML format. Base64 is a great encoding for passing data around over HTTP, but to avoid the stress of the XML parser getting its hands on your data, a CDATA block is always useful.

BTW - Thanks for a great site. I refer a lot of people using CF to your site for advice and tutorials. Keep up the great work!


Jan 4, 2008 at 11:53 AM // reply »
6,516 Comments

@Brian,

Good suggestion, thanks. And also, thanks for the kind words about the blog :)


Jan 4, 2008 at 1:45 PM // reply »
125 Comments

Base64 uses A-Z a-z 0-9 + / and =. There's nothing in there that could invalidate the XML. So yes, you could use CDATA blocks, but it's not needed.

Hopefully that gives you the other 5% Ben. :)

http://en.wikipedia.org/wiki/Base64


Jan 4, 2008 at 1:46 PM // reply »
6,516 Comments

@Elliott,

Perfect! Thanks. I kept trying to find the definition, but all I ever found was that it uses "printable characters", which did not instill 100% confidence.


Jul 21, 2009 at 4:36 PM // reply »
1 Comments

I think the ToBase64() function is deprecated.
You can use:
BinaryEncode(objBinaryData,"Base64");


Jul 21, 2009 at 4:49 PM // reply »
6,516 Comments

@Bilal,

From the documentation it looks like you are absolutely correct. Thanks for pointing that out.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 21, 2009 at 4:49 PM
Styling The ColdFusion 8 WriteToBrowser CFImage Output
Great work yet again Ben! Whilst I didn't use this whole code, I copied some of your regex code for a similar problem with the lack of an alt attribute and unescaped ampersands in CFIMAGE for Railo 3 ... read »
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »
Nov 21, 2009 at 10:56 AM
HostMySite.com Has The Best ColdFusion Hosting
@Mehul, Yes very nice people, however several downtimes per day which was not acceptable. Hence we had to move out. I am glad you are having good luck with them so far. ... read »
Nov 20, 2009 at 11:32 PM
Five Months Without Hungarian Notation And I'm Loving It
I've used headless camel case for years for not only ColdFusion variables, but also SQL tables and fields... pretty much everything involving code. I also subscribe to the "don't abbreviate and clea ... read »
Nov 20, 2009 at 11:00 PM
Five Months Without Hungarian Notation And I'm Loving It
@Marcel, Yeah, I always err on the side of longer but more readable variable names. As for the camel casing of CF methods and the headless camel casing of custom items, I get around this by always ... read »