473,574 Members | 2,309 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to upload a file in Coldfusion

16,027 Recognized Expert Moderator MVP
How to Upload a File in Coldfusion

Use the cffile tag for uploading files to the server.

Note that allowing people to upload files is fraught with danger and only trusted users should be allowed to upload files. Checks should be made to make sure that only allowed file types are uploaded.

The Client-Side

First of all, let us deal with the client side. This assumes some knowledge of HTML.

You need to include an input file field in your form, use the POST method and set the MIME encoding to "multipart/form-data" from the default value of "applicatio n/x-www-form-urlencoded".

[HTML]<form name="uploadfor m" action="actionp age.cfm" method="POST" enctype="multip art/form-data">
<input type="file" name="uploadfil e">
<input type="submit" name="uploadsub mit" value="Upload">

These are the three basic client-side components for a file upload.

The Server-Side

Now onto the server side. This assumes some basic knowledge of Coldfusion.

You will need to use the cffile tag (the syntax is shown below):

Expand|Select|Wrap|Line Numbers
  1. <cffile 
  2.    action = "upload"
  3.    fileField = "formfield"
  4.    destination = "full_path_name"
  5.    nameConflict = "behaviour"
  6.    accept = "mime_type" or "file_type"
  7.    mode = "permission"
  8.    attributes = "file_attribute_or_list"
  9.    result = "result_name">
Only the first three attributes are required, the rest are optional. The last attribute was added in Coldfusion MX 7. The following code demonstrates an example of this tag's use:

Expand|Select|Wrap|Line Numbers
  1. <cffile 
  2.    action = "upload"
  3.    fileField = "uploadfile"
  4.    destination = "c:\Inetpub\wwwroot\uploads"
  5.    nameConflict = "overwrite">
The cffile tag can be used for interacting with server files, but we are interested in uploading, so we set the action attribute to upload. The fileField attribute must correspond with the file input field defined in our form. The destination should be an absolute path to a directory on the server. If you do not specify an absolute path, the destination path will be relative to the Coldfusion temp directory which you can obtain with GetTempDirector y().

The nameConflict attribute is optional, but should be specified in case a file already exists with the same name. In that case, you have four options:
Error: An error message is displayed with the page processing aborted.
Skip: The file is not uploaded, but no error message .
Overwrite: Files on the server with the same name are overwritten.
Makeunique: The file is uploaded and given a unique name.

You can use the accept attribute to restrict the types of files that the user is allowed to upload, e.g. to allow only GIFs, JPGs and PNGs, you could use:
Expand|Select|Wrap|Line Numbers
  1. accept="image/gif,image/jpg,image/png"
Some Optional Attributes

You can skip this part if you don't need to set the file properties.

The final three attributes are mode, attributes and result.

mode lets you set the read/write/execute file permissions on Unix/Linux.

attributes allows you to set whether the file should be readOnly (read only), hidden or normal (on Windows). each attribute must be explicitly set, otherwise they will be overridden.

result lets you use a prefix other than cffile for the variable which contains the result parameters, e.g. if you set this result parameter to cfresult, for the size of the uploaded file, instead of
Expand|Select|Wrap|Line Numbers
  1. cffile.fileSize
you would use
Expand|Select|Wrap|Line Numbers
  1. cfresult.fileSize

Ok, now for an example. Our example sends the form to the same page which we shall call UploadFile.cfm.

Expand|Select|Wrap|Line Numbers
  1.  <cfif isDefined("form.uploadfile")>
  2.  <cffile 
  3.     action = "upload"
  4.     fileField = "uploadfile"
  5.     destination = "c:\Inetpub\wwwroot\uploads"
  6.     nameConflict = "overwrite">
  7.  <cfelse>
  8.   <form name="uploadform" action="UploadFile.cfm" method="POST" enctype="multipart/form-data">
  9.    <input type="file" name="uploadfile">
  10.    <input type="submit" name="uploadsubmit" value="Upload">
  11.   </form>
  12.  </cfif>
Jan 23 '07 #1
3 23053
14 New Member
what of uploading more than one file
Mar 13 '10 #2
16,027 Recognized Expert Moderator MVP
Use more input fields in your HTML code, e.g. uploadfile1, uploadfile2 and so on. In your Coldfusion code, use multiple cffile tags. You could keep a count and use a loop to make things easier.
Mar 15 '10 #3
Ajo Koshy Josep
5 New Member
Multiple file upload can be achieved by using the tag "cffileuplo ad". But this tag is supported only in ColdFusion 9.

May 7 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

by: English Teacher | last post by:
Which would be more useful to learn, PHP or COLDFUSION? I know Coldfusion is popular in the work force. Is PHP? Thanks!
by: dave | last post by:
Hello there, I am at my wit's end ! I have used the following script succesfully to upload an image to my web space. But what I really want to be able to do is to update an existing record in a table in MySQL with the path & filename to the image. I have successfully uploaded and performed an update query on the database, but the problem...
by: worzel | last post by:
is python more popular than coldfusion? I realsie that is a very general question as one thing does not directly relate to the other. My issue is that I am ditching coldfusion due to there being next to no work for it, and I am thinking of taking on python as a second language to java in the hope of improving my resume.
by: William Fields | last post by:
Hello, I'm trying to find out more information about ColdFusion and could not find what I'm looking for on Macromedia's website. My question has more to do with what ColdFusion is and how web services are supported. Specifically, I'd like to know: Is Coldfusion nothing more than an IIS extension like ASP.NET? Can Coldfusion and ASP.NET...
by: SEMIH DEMIR | last post by:
Sitelerden birinde verilen yabancı kaynakli bir scriptti duzenledim yanlız birseyin içinden bir turlu cıkamadım işin aslı ilk defa persistin upload componentini kullanacam yanlız suanki haliyle verdiği hata şu.Bilen arkadaşlar lütfen yardım edin Persits.Upload.1 error '800a0020' The system cannot find the path specified. ...
by: ajaxnewbie | last post by:
Can someone help me convert the PHP lines below from PHP to Coldfusion? <?php $ftmp = $_FILES; $oname = $_FILES; $fname = 'upload/'.$_FILES; if(move_uploaded_file($ftmp, $fname)){ ?> <html><head><script> var par = window.parent.document;
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most obvious of which is the sharing of files. For example, you upload images to a server to share them with other people over the Internet. Perl comes ready...
by: bonneylake | last post by:
Hey Everyone, Well i don't know if my question should be in javascript/ajax or coldfusion, i figure this is more of a coldfusion question. But if this is in the wrong section let me know an all gladly delete my question an put it in the correct section. Well what i am trying to do is pretty simple. I have a input field and someone starts...
by: MoroccoIT | last post by:
Greetings - I saw somewhat similar code (pls see link below) that does mupltiple files upload. It works fine, but I wanted to populate the database with the same files that are uploaded to mydirectory, but for some reason, I am getting different file names on the database. Here is the full code: please do serach on kewword "database" to...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.