The author explains how to upload documents using the CFFILE tag in ColdFusion. He covers making a form that has an infinite number of fields, using CFLOOP. He starts with getting basic functionality, then moves on to the "fun stuff" -- multiple file upload.
Multiple File Upload with CFFILE - Making it Pretty (Page 3 of 3 )
Below is the complete code with some final additions that will do a CFLOCATION back to the form page and display a message on the screen showing which files were uploaded and which form fields were blank.
Let's briefly walk through the entire code and point out the changes. Let's start with the server side. The first new piece of code (line 3) is the CFSET, I am just initializing a list variable that will contain the message sent back to the client. Lines 4 through 10 are the same as before. Line 11 concatenates a message onto the list variable (message). The message we are creating is ",File (somefile.jpg) was uploaded) ". We are putting %20 for space markers (it is the hexadecimal to ascii equivalent). Line 12 is a CFELSE which handles the form fields that are blank. Line 13 creates a message like ", File4 was empty". So it just keeps looping, creating the message until the loop terminates then it goes to line 16, where it does a CFLOCATION back to the client side.
On the client side, the only changes made are to add the CFIF statement at the top (lines 20 to 26) to check if there are any messages sent from client part of the page. If there is a URL.MSG, it loops through the list because when we were creating the Message on the client side we kept on adding commas so that it would be a list (and thus easy to loop through).
So we started out with the goal of creating a versatile form for uploading multiple files. We accomplished the task by using two types of CFLOOP (an index loop and a list loop), and a couple other items of interest as needed. You can now use this form to accomplish all your uploading needs, and can easily change it so it displays one field or one thousand. A note of interest, if you also want to enter a record into a database that the image was uploaded and what the name is, you could put a CFQUERY statement before or after line 11. When you do the insert statement, use #file.serverfile# as the filename and not #form.filename#.
Have fun uploading, while being mindful of your disk space quota of course.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.