|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Uploading Files
|
|
|
|
File uploading requires that you create two files:
- An HTML form to enter file upload information
- An action page containing the file upload code
|
|
|
|
Creating a file upload HTML form |
|
|
|
HTML forms can be designed in most browsers to give users the ability to upload files. Setting the HTML INPUT tag type to "file" instructs the browser to prepare to read and transmit a file from the user's system to your server. Setting the ENCTYPE FORM attribute to "multipart/form-data" tells the server that the form submission contains an uploaded file.
|
|
|
|
Example: An HTML form for file upload
|
|
|
<FORM ACTION="FileUpload.cfm"
ENCTYPE="multipart/form-data"
METHOD="Post">
<PRE>
File Name: <INPUT NAME="FileName" TYPE="text">
File: <INPUT NAME="FileContents" TYPE="file">
<INPUT TYPE="submit" VALUE="Upload File">
</PRE>
</FORM>
The user can enter a file path or browse the system and pick a file to send.
|
|
|
|
Note
|
|
|
The FORM attribute ENCTYPE "multipart/form-data" must be included.
|
|
|
|
Creating a file upload application page |
|
|
|
Submitting a file does not save it on the server. When a file is submitted, it is encoded and sent along with the other form data. The CFFILE tag in the file upload application page decodes the file and saves its contents on the server.
|
|
|
|
Example: Upload a file
|
|
|
The following example shows a CFFILE tag that could be placed in the "FileUpload.cfm" file referred to in the previous example:
<CFFILE ACTION="UPLOAD"
FILEFIELD="FileContents"
DESTINATION="C:\Web\Uploads\">
If the file upload form that requested this application page sent a file named KeyMemo.doc, the file is saved to the server as:
c:\Web\Uploads\KeyMemo.doc
To save the file under a different name, specify the new file name in the DESTINATION attribute. In this example, "KeyMemo.doc" is saved on the server as "UploadedFile.doc":
<CFFILE ACTION="UPLOAD"
FILEFIELD="FileContents"
DESTINATION="C:\Web\Uploads\UploadedFile.doc">
You could also make any of these attributes dynamic variables. For example, you could set the file name based on information from a database query.
|
|
|
|
Note
|
|
|
The FILEFIELD attribute expects the name of a form field, not the contents of the form field, so you should not enclose the form field in # signs .
|
|
|
|
Resolving conflicting file names |
|
|
|
When a file is saved to the server, there is a risk that another file may already exist with the same name. In the event of this occurrence, there are a number of actions you can take using the NAMECONFLICT attribute .
|
|
|
|
Example: Resolving a name conflict
|
|
|
The following example will create a unique file name, while retaining the file extension, if there is a name conflict when the file is uploaded:
<CFFILE ACTION="Upload"
FILEFIELD="FileContents"
DESTINATION="C:\Web\Uploads\"
NAMECONFLICT="MAKEUNIQUE">
|
|
|
|
Controlling the type of file uploaded |
|
|
|
For some applications, you might want to restrict the type of file that is uploaded. For example, you may not want to accept graphic files in a document library.
The ACCEPT attribute is used to restrict the type of file that will be allowed in an upload. When an ACCEPT qualifier is present, the uploaded file's MIME content type must match the criteria specified or an error will occur. ACCEPT 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.
|
|
|
|
Example: Restricting file types
|
|
|
This CFFILE specification will only save an image file that is in the GIF format:
<CFFILE ACTION="Upload"
FILEFIELD="UploadFile"
DESTINATION="c:\uploads\MyImage.GIF"
NAMECONFLICT="OVERWRITE"
ACCEPT="image/gif">
This CFFILE specification will only save an image file that is either a GIF or a JPEG :
<CFFILE ACTION="Upload"
FILEFIELD="UploadFile"
DESTINATION="c:\uploads\MyImage.GIF"
NAMECONFLICT="OVERWRITE"
ACCEPT="image/gif, image/jpeg">
This CFFILE specification will only save an image file, but the format doesn't matter:
<CFFILE ACTION="Upload"
FILEFIELD="UploadFile"
DESTINATION="c:\uploads\MyImage.GIF"
NAMECONFLICT="OVERWRITE"
ACCEPT="image/*">
|
|
|
|
Note
|
|
|
Any file will be saved if ACCEPT is omitted, left empty, or contains "*/*".
|
|
|
  
|
|
|
AllaireDoc@allaire.com
Copyright © 1998, Allaire Corporation. All rights reserved.
|