PDA

View Full Version : cffile upload example limiting upload to .csv files only



Brian
November 20th, 2011, 02:57 PM
Here is an example of using ColdFusion to upload a .csv file and only accepting .csv files.

NOTE: You can accept other specific files by changing the "accept" values. For safety and security it is recommended to limit the file upload types to just what you are expecting in your application.


<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ColdFusion cffile tag example - limiting file upload types</title>
</head>

<body>
<h2 style="color:Blue">ColdFusion cffile tag example: Upload - accepting only .csv files</h2>

<cfset UploadFolder="D:\home\domain.com\wwwroot\uploads">
<cfif IsDefined("Form.UploadFile") AND Form.UploadFile NEQ "">
<cffile
action="upload"
filefield="UploadFile"
destination="#UploadFolder#"
nameconflict="overwrite"
accept = "text/csv, application/csv"
>
The file was uploaded successfully!
<br />
Uploaded file name: <cfoutput>#cffile.ClientFile#</cfoutput>
<cfelse>
Select the file that you want to upload:
</cfif>

<form name="UploadForm" method="post" enctype="multipart/form-data" action="">
<input type="file" name="UploadFile">
<input type="submit" name="submit" value="Upload"/>
</form>

</body>
</html>