Notice: With the launch of Adobe Cookbooks, this site will no longer be accepting new entries or posting new content. Thanks to everyone who submitted content!

How do I upload a file to my ColdFusion application by way of a form?

Using a HTML form to upload files is a 2-step process. The first step involves the use of a HTML form to collect the file data from the user. Once you have the file on your web server, the second step involves the use of <cffile>.

If you know that your HTML form will contain a type = "file" form-field, you need to be sure to set the <form> enctype parameter to "multipart/form-data". This vital step ensures that non-text file data can be transmitted with the form post. The second step in setting up a HTML form to accept a file is to include a type= "file" form-field. It is important to note that the file upload feature is browser specific and not supported by all browsers- especially older browsers. It is also important to note that different browsers and operating systems may render the file input form differently. If your application must support multiple browsers and operating systems, you will want to be sure to test your type = "file" form-field code with each browser and operating system. A sample file upload form would look as follows:

<form action="fileUpload.cfm" method="post" enctype="multipart/form-data">
<input name="fileField" type="file">
<input type="submit" value="upload">
</form>

As already mentioned, the next step in the process is to use some ColdFusion tags and functions to capture the now posted form data. This is accomplished by using the <cffile> tag with the action = "upload" attribute, and the fielfield attribute set to the type = "file" form-field you just posted. This code would look as follows:

<cffile
action="upload"
destination="C:/Temp/"
nameconflict="overwrite"
filefield="fileField">


This question was written by Jeremy Petersen.
It was last updated on January 26, 2006 at 10:16:40 AM EST.

CFML Referenced

<cffile>

Categories

Forms

Comments

Comment made by Mark on January 26, 2006 at 1:52 PM
What about file permissions on the directory that the file gets droped into? You usually have to open the directory up so that files can be put there.


Comment made by Dan Sorensen on February 3, 2006 at 10:53 PM
Is it possible to add a "Browse File" button in the first form?


Comment made by Jeremy Petersen on February 3, 2006 at 10:58 PM
By having type="file" in the form you get a file upload text box and Browse button.


Comment made by Robert Owen on February 9, 2006 at 11:06 AM
Ok. What if you want to collect data on a form to place in a dbase and upload a picture to a folder and place the name of the file uploaded into the dbase. How would you layout the action page?


Comment made by Jeremy Petersen on February 9, 2006 at 11:43 AM
Robert, your request is too much for a single recipe entry. If you want to break your request up into multiple parts and submit them we can try to add them in.


Comment made by Robert Owen on February 9, 2006 at 12:31 PM
Thanks... Just figured it out.


Comment made by Nate on March 13, 2007 at 10:20 AM
Can you please explain how this works if you were to use the variable form.filedata? I can't seem to cfdump that variable to see it's structure or learn more about it.