Uploading Files

File uploading requires that you create two files:

To create an HTML file to specify file upload information:

  1. Create a new file in ColdFusion Studio.
  2. Modify the file so that it appears as follows:
    <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>
    
    
  3. Save the file as uploadfileform.cfm in myapps under the Web root directory.

Reviewing the code

The following table describes the code and its function:
Code
Description

<form action="uploadfileaction.cfm" 

  enctype="multipart/form-data" 

  method="post"> 

Create a form that contains file selection fields for upload by the user. The enctype attribute value tells the server that the form submission contains an uploaded file

<input type="file" 

  name="FiletoUpload" 

  size="45"> 

Allow the user to input a field. The file type instructs the browser to prepare to read and transmit a file from the user's system to your server and automatically includes a Browse button to allow the user to look for the file instead of entering the entire path and filename.

The user can enter a file path or browse the system and pick a file to send.

To create an action page to upload the file:

  1. Create a new file in ColdFusion Studio.
  2. Modify the file so that it appears as follows:
    <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>
    
    
  3. Change the following line to point to an appropriate location on your server:
    destination="c:\temp"
    
    
  4. Save the file as uploadfileaction.cfm in myapps under the Web root directory.
  5. View uploadfileform.cfm in your browser, enter values and submit the form.

    The file you specified is uploaded.

Reviewing the code

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.


Resolving conflicting filenames

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.

Controlling the type of file uploaded

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.


Example: Restricting file types

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 accept attribute, leave it empty, or specify "*/*".




Banner.Novgorod.Ru