I'm handling a form post in a CFC but I can't get file upload to work. How do you use <cffile> in a CFC?The key here is that <cffile> expects a form field name for the filefield= attribute. You'd normally say:
<form enctype="multipart/form-data" ...>
<input type="file" name="userfile" ... />
...
<cffile action="upload" filefield="userfile" ... />
What people often try to do is pass the form fields into the CFC as arguments to a method and then they cannot figure out what to put in the filefield= attribute.
Inside the CFC, you still need the name of the field in the <cffile> tag when you specify filefield=. The easiest way to do this is to pass the name of the form field to the method:
<input type="file" name="userfile" ... />
...
<cffile action="upload" filefield="userfile" ... />
service.uploadDocument("userfile");
and then inside the method:
<cfargument name="formfield" ... />
...
<cffile action="upload" filefield="#arguments.formfield#" ... />
Note the #..# there to evaluate the argument to get the string passed in - the form field name.
This is based on Todd Rafferty's post to cf-talk which was the clearest explanation I found via Google!...
<cffile action="upload" filefield="#arguments.formfield#" ... />

12 responses so far ↓
1 Michael Long // Jan 9, 2009 at 2:21 PM
2 Sean Corfield // Jan 9, 2009 at 3:04 PM
3 Todd Rafferty // Jan 9, 2009 at 3:19 PM
4 Todd Rafferty // Jan 9, 2009 at 3:24 PM
5 Sean Corfield // Jan 9, 2009 at 4:14 PM
6 Todd Rafferty // Jan 9, 2009 at 5:59 PM
7 Adam Bellas // Jan 10, 2009 at 10:48 AM
It seems clear that ColdFusion needs to read the multipart/form-data (or one of it's "parts"?) directly from the http stream... am I getting that right? It feels un-encapsulated when I'm making this work properly as you've documented in this post.
So why is it, exactly, that the binary stream can't be, or isn't, passed in as an argument? I'm guessing the answer lies in the way the http POST mechanism works between the browser and the http server.
8 Todd Rafferty // Jan 10, 2009 at 1:02 PM
I guess you could create a pointer, but at that point, isn't form.whateverfield already enough of a pointer?
9 Adam Bellas // Jan 10, 2009 at 2:00 PM
The way I see it, if you have a CFC that's handling the upload, and it's being called by (let's say we're using Fusebox, it's what I know) an act_handleUpload.cfm, which is called by the framework, which is what was initially hit by the POST request from the browser. Perhaps I've got the encapsulation concepts wrong, but it seems out-of-place to me for the CFC layer to be dependent on anything other than what act_handleUpload.cfm is providing it. The form scope would be one of those "other" things the CFC isn't responsible for knowing. No?
10 Todd Rafferty // Jan 10, 2009 at 5:12 PM
11 Sean Corfield // Jan 11, 2009 at 10:33 AM
It's not that different to a CFC that accesses some system resource being passed the string that identifies that resource.
12 Joe Tseng // Apr 15, 2009 at 7:05 PM
Leave a Comment