patternMinor
Uploading multiple files for faculty nomination system
Viewed 0 times
uploadingfacultynominationsystemfilesformultiple
Problem
I created a few forms for faculty members to nominate colleagues for awards. Many of the nominations require documentation to be submitted in order to validate the nomination. I created one form then modified it to fit the needs of many different award forms. I created it rather hastily and would like input as to how I may trim the fat and make the code more efficient.
One of the forms can be found here. Here's the code relevant to file uploading (starting at line 376 of the paste):
One of the forms can be found here. Here's the code relevant to file uploading (starting at line 376 of the paste):
Solution
Considering that you're doing this:
over and over and over again. I'd create a custom function to handle these steps for you. Any place you have something that needs to be dynamic, create an Argument for that value. Then you could replace the above code with something like this:
Then if you ever need to change how your file uploads are handled, you can just change the one function to make that happen.
Good rule of thumb: any time you copy and paste a block of code, and then replace one or two strings, make it a function.
over and over and over again. I'd create a custom function to handle these steps for you. Any place you have something that needs to be dynamic, create an Argument for that value. Then you could replace the above code with something like this:
uploadDocument(destination = field = "recommendation1");
uploadDocument(field = "recommendation2");
uploadDocument(field = "recommendation3");
uploadDocument(field = "recommendation4");Then if you ever need to change how your file uploads are handled, you can just change the one function to make that happen.
Good rule of thumb: any time you copy and paste a block of code, and then replace one or two strings, make it a function.
Code Snippets
<!---Upload document to the destination. Accept only MSWord, PDF, RTF and plain text files.--->
<cffile action="upload"
filefield="recommendation4"
accept="application/msword, application/pdf, application/rtf, text/plain, application/vnd.ms-word.document.12, application/vnd.openxmlformats-officedocument.wordprocessingml.document"
destination="#destination#"
nameconflict="makeunique">
<!--- Create variable to reference the original document name and extension uploaded from client.--->
<cfset clientRecommendation4 = #file.ClientFile#>
<!---Create variable to reference renamed document. Retain orignal file extension.--->
<cfset renameRecommendation4 = "recommendation4"&"."&#cffile.clientFileExt#>
<!---Rename uploaded document using variable. Save renamed document to original destination.--->
<cffile action="rename"
source="#destination##File.ServerFile#"
destination="#destination##Trim(renameRecommendation4)#">uploadDocument(destination = field = "recommendation1");
uploadDocument(field = "recommendation2");
uploadDocument(field = "recommendation3");
uploadDocument(field = "recommendation4");Context
StackExchange Code Review Q#1460, answer score: 3
Revisions (0)
No revisions yet.