Throwing And Catching A File Using CFHttp For Both Actions

Posted April 2, 2007 at 1:25 PM

Tags: ColdFusion

I can't remember where I heard this, but not so long ago I heard or read about someone who wanted to send a file to another server using CFHttp. He didn't have any FTP stuff set up, so that was they best idea he had. This seemed like a fun little piece of functionality to test as I have never done that before.

I set up two pages: cfhttp_throw.cfm and cfhttp_catch.cfm. As you can probably guess, the cfhttp_throw.cfm takes a file from the local file system and "posts" it to the cfhttp_catch.cfm. The cfhttp_catch.cfm file then "uploads" it to its server and echoes back the file name as it was stored on the new machine.

This was suprisingly easy. I have not used CFHttp all that extensively and in about 10 minutes I worked out something that did just what I wanted to do. And, in the process, I used a CFHttpParam tag of type "File" which I had never done before. I have to go back and check out the rest of the CFHttpParam types to see what else I am missing (damn you ignorance!).

Here is the cfhttp_throw.cfm code. It doesn't have all the CFTry / CFCatch tags that probably should be used, but for this proof of concept, it was not necessary:

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

  • <!---
  • Submit the file the CFHTTP_Catch.cfm file. Notice that we
  • are sending the file via a CFHttpParam FILE tag. By using
  • this file param, CFHTTP automatically sends all form
  • fields as multi-part form data; therefore, we do NOT need
  • to specify the "multipart='true'" CFHttp attribute.
  • --->
  • <cfhttp
  • url="http://swoop/..../cfhttp_catch.cfm"
  • method="POST"
  • useragent="Mozilla/5.0 Gecko/20070309 Firefox/2.0.0.3"
  • result="objHTTP">
  •  
  • <!---
  • Send along a file via the FORM post. This acts the same
  • as a stanard form Input type="file" field and can be
  • handled as such on the "Catch" page.
  • --->
  • <cfhttpparam
  • type="FILE"
  • name="file"
  • file="#ExpandPath( './test.jpg' )#"
  • />
  •  
  • </cfhttp>
  •  
  •  
  • <cfoutput>
  •  
  • <h4>
  • CFHttp Post Result:
  • </h4>
  •  
  • <p>
  • #objHTTP.FileContent#
  • </p>
  •  
  • </cfoutput>

And, here is the cfhttp_catch.cfm code:

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

  • <!--- Kill extra output. --->
  • <cfsilent>
  •  
  • <!---
  • Param the form fields. Since this data is coming via
  • a CFHTTP "Post" we can operate as if it was a standard
  • form submission.
  • --->
  • <cfparam
  • name="FORM.file"
  • type="string"
  • default=""
  • />
  •  
  •  
  • <!--- Try to upload the file. --->
  • <cftry>
  •  
  • <!---
  • Upload the file. When defining the CFFile, we can
  • treat the posted file as if it was submitted via a
  • standard File Input (since it was posted as a FILE
  • using CFHttpParam).
  • --->
  • <cffile
  • action="UPLOAD"
  • filefield="file"
  • destination="#ExpandPath( './files/' )#"
  • nameconflict="MAKEUNIQUE"
  • />
  •  
  •  
  • <!---
  • Echo back the name of the file as it has been
  • saved to the server.
  • --->
  • <cfcontent
  • type="text/plain"
  • variable="#ToBinary( ToBase64( CFFILE.ServerFile ) )#"
  • />
  •  
  •  
  • <!---
  • Catch any errors that were thrown during the
  • file upload.
  • --->
  • <cfcatch>
  •  
  • <!--- Echo back the error message. --->
  • <cfcontent
  • type="text/plain"
  • variable="#ToBinary( ToBase64( CFCATCH.Message ) )#"
  • />
  •  
  • </cfcatch>
  • </cftry>
  •  
  • </cfsilent>

That was pretty cool. I like that using the proper CFHttp / CFHttpParam tags, your target page can act as if the request was coming from a standard Form-action page with standard FORM-scoped values. I think it's totally awesome that CFFile can actually upload the file that is posted to it. Very cool.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page





Reader Comments

Apr 2, 2007 at 1:51 PM // reply »
226 Comments

What is the deal with this?

#ToBinary( ToBase64( CFFILE.ServerFile ) )#

CFFILE.* should all be strings. So why not just output as is?


Apr 2, 2007 at 3:56 PM // reply »
7,490 Comments

@Ray,

Cause I am a sick person .... the Variable attribute of CFContent only takes binary data and won't allow string data unless you convert it to binary first (hence the ToBinary stuff). Sure, I could just output it after the CFSilent, but I don't think that looks as nice.... Like I said, I am sick (in the head) and code formatting is that special to me :)


Apr 2, 2007 at 3:57 PM // reply »
7,490 Comments

Plus, I just really like using CFContent. Feels so good.


Apr 2, 2007 at 4:29 PM // reply »
226 Comments

Hah, that is insane.


Apr 2, 2007 at 4:55 PM // reply »
7,490 Comments

What fun would "sane" be ;)


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 14, 2010 at 3:56 PM
Parsing CSV Values In To A ColdFusion Query
@Ben Nadel, Ben, you're right. I have embedded delimiters, which this function does not handle. I also need to specify the column headers, which yours does not handle. Decisions, decisions. ... read »
Mar 14, 2010 at 2:01 PM
Creating UI Elements With Low-Coupling And Conditional Event Handling
@Ryan, It's definitely an interesting approach. I'm used to binding to objects / elements that the idea of binding to an event object is a bit much to wrap my head around just yet. As far as the TH ... read »
Mar 14, 2010 at 10:08 AM
The Magic Of Thinking Big By David Schwartz (Thanks Clark Valberg!)
@Kamal, You can get it off of iTunes / Audible.com. ... read »
Mar 13, 2010 at 9:14 PM
The Magic Of Thinking Big By David Schwartz (Thanks Clark Valberg!)
Ben, COuld you plz tell me how to find the audio book version of The Magic of Thinking Big by David J. Schwartz, Ph.D Thanks ... read »
sun
Mar 13, 2010 at 6:21 PM
Sending (SMS) Picture Messages With ColdFusion And CFMail
MY BAD! @tmomail.net IS correct ... but, ONLY from pic taken W/ a cell -- NOT as attachment in an email ... (took ~4 min. for it to arrive ... then would NOT DL to my cell) ... read »
sun
Mar 13, 2010 at 6:16 PM
Sending (SMS) Picture Messages With ColdFusion And CFMail
@tmomail.net is NOT correct! IS t-mobile addy ... but, ONLY for a txt msg! ... read »
Mar 12, 2010 at 8:24 PM
Creating UI Elements With Low-Coupling And Conditional Event Handling
@Ben, Yes, that's exactly what's happening. That's the approach YUI has taken with custom events, you subscribe the handler to the Event object itself. I really like the event system in YUI, it's ... read »
Mar 12, 2010 at 7:34 PM
FLV 404 Error On Windows 2003 Server
I spent a good couple of hours on this today. What a pain. On my old server, everything was fine. I migrated the site to a new server, and suddenly, all the files with audio didn't work. Just got ... read »