Upload a File Preserve Name of File Coldfusion

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 lawmaking

To create an HTML file to specify file upload information:

  1. Create a new file in ColdFusion Studio.
  2. Alter the file and so that it appears as follows:
    <html>  <head>    <title>Specify File to Upload</title>  </head>    <body>  <h2>Specify File to Upload</h2>  <course activity="uploadfileaction.cfm"       enctype="multipart/course-data"       method="post">    <p>Enter the complete path and filename of the file to upload:    <input type="file"        proper name="FiletoUpload"        size="45">    </p>    <input   type="submit"        value"Upload">  </form>  </trunk>  </html>          
  3. Save the file as uploadfileform.cfm in myapps under the Web root directory.

Reviewing the code

The following tabular array describes the lawmaking and its office:

Code

Clarification

                    <form activeness="uploadfileaction.cfm"     enctype="multipart/form-data"     method="postal service">                 
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"     proper noun="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 Scan button to permit the user to look for the file instead of entering the entire path and filename.

The user tin 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>    <torso>  <h2>Upload File</h2>    <cffile activeness="upload"      destination="c:\temp"      nameConflict="overwrite"      fileField="Form.FiletoUpload">          <cfoutput>  Y'all uploaded the file #cffile.ClientFileName#.#cffile.ClientFileExt#        successfully to  #cffile.ServerDirectory#\#cffile.ServerFileName#.#cffile.  ServerFileExt#.  </cfoutput>    </body>  </html>          
  3. Change the following line to indicate to an appropriate location on your server:
    destination="c:\temp"          
  4. Save the file equally uploadfileaction.cfm in myapps under the Web root directory.
  5. View uploadfileform.cfm in your browser, enter values and submit the course.

    The file y'all specified is uploaded.

Reviewing the code

The following table describes the code and its role:

Lawmaking

Clarification

                    <cffile activeness="upload"                 
Set up 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 practise non 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 data on cffile scope variables, see "Evaluating the Results of a File Upload".


Note

This instance performs no error checking and does not incorporate any security measures. Before deploying an application that performs file uploads, be sure to contain both error handling and security.


Resolving conflicting filenames

When y'all salve a file to the server, in that location is a risk that some other file might already exist with the same proper name. In this instance, there are a number of actions that you can have using the nameConflict attribute. For instance, 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 non resemble the attempted name.

Decision-making the type of file uploaded

For some applications, you might want to restrict the type of file that is uploaded. For instance, y'all might not want to accept graphic files in a certificate library.

Yous 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'southward MIME content blazon must friction match the criteria specified or an error occurs. The accept attribute takes a comma-separated listing 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.


Notation

Not all browsers back up MIME type associations.


Case: Restricting file types

This cffile specification saves an paradigm file simply if it is in the GIF format:

<cffile action="Upload"      
        fileField="Class.FiletoUpload"    destination="c:\uploads\MyImage.GIF"    nameConflict="Overwrite"    have="prototype/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, paradigm/jpeg">      

This cffile specification saves whatever image file, regardless of the format:

<cffile activity="Upload"      
        fileField="Form.FiletoUpload"    destination="c:\uploads\MyImage.GIF"    nameConflict="Overwrite"    accept="image/*">      

Note

ColdFusion saves any uploaded file if yous omit the accept attribute, leave it empty, or specify "*/*".




Banner.Novgorod.Ru

reillybelarrine.blogspot.com

Source: http://doc.novsu.ac.ru/ColdFusion/Developing_ColdFusion_Applications/manageFiles3.html

0 Response to "Upload a File Preserve Name of File Coldfusion"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel