|
The <cffile> tag uses various attributes to allow files to be uploaded to the server, or to restrict them from being uploaded. The attributes nameConflict and accept allow you to restrict the types of files to be uploaded.
The nameConflict attribute is used to resolve filename conflicts when a file is uploaded to a specified directory. This attribute can set to the following four values:
Error. Instructs ColdFusion not to upload the file and returns an error to be displayed in the client Web browser
Overwrite. Instructs ColdFusion to overwrite an existing file with the same filename in the specified directory on the server
MakeUnique. Instructs ColdFusion to generate a unique filename for the uploaded file, so that the filename doesn't conflict with the filename of an existing file
Skip. Instructs ColdFusion to skip the uploading of the file without generating an error
The following code uses the MakeUnique attribute to save the file in the server directory with a unique filename if an existing file with the same filename is found on the same directory:
<html> <head> <title>Upload File</title> </head> <body> <h2>Upload File</h2> <cffile action="upload" destination="d:\CfusionMX\wwwroot\uploads\" nameConflict="MakeUnique" fileField="Form.Upload_File"> </body> </html>
Note | When the nameConflict attribute of the <cffile> tag is set to MakeUnique, the ColdFusion server saves the file by appending 1 to the filename. For example, if the server computer directory contains a file named readme.doc, and you upload a file with the same filename, ColdFusion saves the new file as readme1.doc. |
To retrieve the unique filename generated when the nameConflict attribute is set to MakeUnique in the ColdFusion page, you can use the cffile.serverFilename and cffile.serverFileext upload attributes. The following code illustrates the use of these attributes to retrieve the unique filename of the uploaded file:
<html> <head> <title>Using the NameConflict Attribute</title> </head> <body> <h2>Uploaded File</h2> <cffile action="upload" destination="D:\CfusionMX\wwwroot\uploads\" nameConflict="MakeUnique" fileField="Form.Upload_File"> <cfoutput> You uploaded #cffile.ServerFileName#.#cffile.ServerFileExt# successfully to #cffile.ServerDirectory#. </cfoutput> </body> </html>
The accept attribute is used to impose file upload restrictions by specifying the MIME type of the file that can be uploaded to the server.
Note | The MIME type informs an application about data contained in a file. Generally, the MIME type is used in operating systems to associate a file of a specified file extension with an application. For example, all .doc files are opened using Microsoft Word in the Windows operating system. |
The following code uses the accept attribute to specify that only Word document files can be uploaded to the server:
<html> <head> <title> Upload File </title> </head> <body> <h2>Only Upload Word Documents</h2> <cffile action="upload" destination="D:\CfusionMX\wwwroot\uploads\" nameConflict="Overwrite" fileField="Form.Upload_File" accept="application/msword" > </body> </html>
You can specify more than a single MIME type of the file to be uploaded by setting the accept attribute to a comma-separated list of MIME types, as illustrated in the following code:
<html> <head> <title> Upload File </title> </head> <body> <h2>You Can Upload Word Documents, JPEG, and GIF Images</h2> <cffile action="Upload" fileField="Form.Upload_File" nameConflict="Overwrite" destination="D:\CfusionMX\wwwroot\uploads\" accept="application/msword, image/jpeg, image/gif"> </body> </html>
|