File uploading requires that you create two files:
<html> <head> <title>Specify File to Upload</title> </head> <body> <h2>Specify File to Upload</h2> <form action="uploadfileaction.cfm" enctype="multipart/form-data" method="post"> <p>Enter the complete path and filename of the file to upload: <input type="file" name="FiletoUpload" size="45"> </p> <input type="submit" value"Upload"> </form> </body> </html>
uploadfileform.cfm
in myapps
under the Web root directory.The following table describes the code and its function:
The user can enter a file path or browse the system and pick a file to send.
<html> <head> <title>Upload File</title> </head> <body> <h2>Upload File</h2> <cffile action="upload" destination="c:\temp" nameConflict="overwrite" fileField="Form.FiletoUpload"> <cfoutput> You uploaded the file #cffile.ClientFileName#.#cffile.ClientFileExt# successfully to #cffile.ServerDirectory#\#cffile.ServerFileName#.#cffile. ServerFileExt#. </cfoutput> </body> </html>
destination="c:\temp"
uploadfileaction.cfm
in myapps
under the Web root directory.uploadfileform.cfm
in your browser, enter values and submit the form.The file you specified is uploaded.
The following table describes the code and its function:
Code |
Description |
---|---|
<cffile action="upload" |
Prepare to upload a file to the server. |
destination="c:\temp" |
Specify the destination of the file. |
nameConflict="overwrite" |
If the file already exists, overwrite it. |
fileField="Form.FiletoUpload"> |
Specify the name of the file to upload. Note that you do not enclose the variable in pound signs. |
You uploaded the file #cffile.ClientFileName#.#cffile. ClientFileExt# successfully to #cffile.ServerDirectory#\#cffile. ServerFileName#.#cffile.ServerFileExt#. |
Inform the user of the file that was uploaded and its destination. For information on cffile scope variables, see "Evaluating the Results of a File Upload". |
Note This example performs no error checking and does not incorporate any security measures. Before deploying an application that performs file uploads, be sure to incorporate both error handling and security. |
When you save a file to the server, there is a risk that another file might already exist with the same name. In this case, there are a number of actions that you can take using the nameConflict
attribute. For example, you can specify the parameter nameConflict="makeunique"
in the cffile
tag to create a unique filename while keeping the file extension the same. The unique name might not resemble the attempted name.
For some applications, you might want to restrict the type of file that is uploaded. For example, you might not want to accept graphic files in a document library.
You use the accept
attribute to restrict the type of file that you allow in an upload. When an accept
qualifier is present, the uploaded file's MIME content type must match the criteria specified or an error occurs. The accept
attribute takes a comma-separated list of MIME data names, optionally with wildcards.
A file's MIME type is determined by the browser. Common types, like image/gif and text/plain, are registered in your browser.
Note Not all browsers support MIME type associations. |
This cffile
specification saves an image file only if it is in the GIF format:
<cffile action="Upload"
fileField="Form.FiletoUpload" destination="c:\uploads\MyImage.GIF" nameConflict="Overwrite" accept="image/gif">
This cffile
specification saves an image file only if it is in GIF or JPEG format:
<cffile action="Upload"
fileField="Form.FiletoUpload" destination="c:\uploads\MyImage.GIF" nameConflict="Overwrite" accept="image/gif, image/jpeg">
This cffile
specification saves any image file, regardless of the format:
<cffile action="Upload"
fileField="Form.FiletoUpload" destination="c:\uploads\MyImage.GIF" nameConflict="Overwrite" accept="image/*">
Note ColdFusion saves any uploaded file if you omit the |