Coldfusion: allowing max file size when uploading

Introduction:

As a web developer, you will build routines or functions to allow the user to upload an image, xls file, pdf file, etc. It is always a good rule, to limit the max file size that the user is uploading. This will eliminate other issues that may arise due to a large file being uploaded. One that comes to mind, is a timeout issue, which than leads to a frustrated user. Try to build your web applications with the user in mind. Limit any possible pitfalls that could occur on the user side.

Coldfusion Code:

Below is the sample Coldfusion code when the file is uploaded.

<cfscript>
// if the form is submitted, do the following
</cfscript>

<CFIF isdefined ("uploadfile")>

    <CFIF VAL(CGI.CONTENT_LENGTH) GT 20000>

         <cflocation addtoken="no"
                         url="index.cfm?Note=File too Large! (#VAL(CGI.CONTENT_LENGTH/1000)# KB)">
         <CFABORT>

    <CFELSE>

       <CFFILE 
                ACTION="Upload" 
                FILEFIELD="whatfile" 
                DESTINATION="C:\customer\uploads\" 
                NAMECONFLICT="OVERWRITE">

       <cflocation addtoken="no"
                       url="index.cfm?Note=File Uploaded (#VAL(CGI.CONTENT_LENGTH/1000)# KB)">
       <CFABORT>

    </CFIF>

</CFIF>

HTML Form Code:

below is the sample HTML for the form that will actually upload the file.

<cfparam name="URL.Note" default="">
<CFOUTPUT>
   <strong><font color="RED">#URL.NOTE#</font></strong>
</CFOUTPUT>

<cfform action="index.cfm?uploadfile=yes" method="post" enctype="multipart/form-data">
What File:
<BR>
<cfINPUT NAME="WhatFile" TYPE="File" SIZE="60" required="yes" message="Please select file">
<BR>
<input type="Submit" value="Upload File">
</cfform>

More by this Author


Click to Rate This Article
working