Despite knowing the right way to do this, I find myself almost every time I use the all-too awesome <cffile> tag in Coldfusion to upload a file passing the value of the file input form field. So for example, let’s assume we have the following HTML form.
HTML Upload Form
<form name="myform" method="post" action="coldfusion_script.cfm" enctype="multipart/form-data"> Upload File:<br /> <input type="file" name="uploadfield" /><br /><br /> <input type="submit" name="submit" value=Submit /> </form>
When we submit our value we run the coldfusion_script.cfm or whatever your script file is. In the script file we run the <cffile> tag with an action=”upload”. This tag takes several arguments, one of which is the ‘filefield’. This is where I initially go wrong every time. Naturally I upload the value of the field in the FORM scope. This is wrong. So I always initially (and incorrectly) write my tag like the following.
Don’t do it this way!
<cffile action="upload" filefield="#FORM.uploadfield#" destination="path/to/destination/directory" nameconflict="overwrite" result="theresult">
The problem here is in the ‘filefield’ argument. I’ve passed in the value of the form field named ‘upload’. Coldfusion doesn’t need me to pass in the value. Instead it needs me to just tell it the name of the file upload field. Running this will return an error that returns the path to the temp directory and let’s you know that the field ‘did not contain a file’. So we should change our cffile tag to this.
Do it this way!
<cffile action="upload" filefield="uploadfield" destination="destination/directory" nameconflict="overwrite" result="theresult">
That’s it. No big thing. But it trips me up all the time.
At Monserrate Monastery in Bogotá, Colombia.