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 »
And, here is the cfhttp_catch.cfm code:
Launch code in new window » Download code as text file »
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
Comments (5) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
What is the deal with this?
#ToBinary( ToBase64( CFFILE.ServerFile ) )#
CFFILE.* should all be strings. So why not just output as is?
Posted by Raymond Camden on Apr 2, 2007 at 1:51 PM
@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 :)
Posted by Ben Nadel on Apr 2, 2007 at 3:56 PM
Plus, I just really like using CFContent. Feels so good.
Posted by Ben Nadel on Apr 2, 2007 at 3:57 PM
Hah, that is insane.
Posted by Raymond Camden on Apr 2, 2007 at 4:29 PM
What fun would "sane" be ;)
Posted by Ben Nadel on Apr 2, 2007 at 4:55 PM