After posting a while back about uploading a file and emailing it using ColdFusion, I was asked to put up a demo of how to upload multiple files using ColdFusion. For this demo, I am concentrating only on the upload aspect and not worrying about any emailing functionality as I believe that adding that is easy after the upload is complete.
Before I show the code, I just want to preempt some stuff. For starters, I am using a LOT Of error handling via ColdFusion's CFTry / CFCatch tags. In practice, I don't really use that much error handling. However, I am just trying to instill some good practice here and drive home the fact that when dealing with a third party service (the file system), there is always potential for errors to occur. And, the truth is, I should really use better error handling anyway.
That being said, this code is only partially tested as I could not generate any errors on the file upload. I think the error handling looks good, but again, not fully tested. The best that I could do was to change the NameConflict attribute in the CFFile tag from MakeUnique to Error and upload two files of the same name, which threw the following error:
There was a problem uploading file #2: File overwriting is not permitted in this instance of the CFFile tag.
So, it seems to be working; but, I just want to stress that I couldn't generate a file system error and therefore some of this code is still theoretical.
Additionally, I build the page such that the number of files to be uploaded can be variable. Right now, it is set to have 5 file fields, but that can be set using the REQUEST.FileCount variable. I know that this kind of stuff is handled very Web 2.0 dynamic style these days, but for this demo, I wanted to keep it really simple and straight forward.
That being said, here is the code:
Launch code in new window » Download code as text file »
Hope that helps. Please let me know if there are any follow up questions.
Download Code Snippet ZIP File
Comments (4) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
Creating A "Down For Maintenance" Page Using Application.cfc
The Fresh Face Of The New York ColdFusion User Group (NYCFUG)
Just wondering why you add the step="1" attribute on all your cfloops?
Posted by duncan on Jan 8, 2008 at 11:06 AM
@Duncan,
There is no technical reason for this. I know that the loop increment defaults to 1. However, since many of my posts are meant to help people learn, I believe that it is best to spell everything out explicitly so there is no mystery and people can concentrate on the real task at hand.
Posted by Ben Nadel on Jan 8, 2008 at 11:11 AM
A nice addition for the beginners out there might be to use the result attribute and show'em how it can be used to interact with the db.
Great post!
Will
Posted by Will on Jan 8, 2008 at 11:02 PM
thank you for this, very helpful
Posted by Glyn on Feb 6, 2008 at 7:04 AM