Загрузки нескольких файлов для системы факультет номинации


Я создал несколько форм для преподавателей номинировать коллег за награды. Многие из номинаций требуют документов, которые будут представлены в целях подтверждения номинации. Я создал одну форму, затем изменил его, чтобы соответствовать потребностям различных формах награду. Я создал его наспех и как бы вход как я могу обрезать жир и сделать код более эффективным.

Одной из форм может быть найден здесь. Вот код, соответствующий для загрузки файлов (начиная в строки 376 пасты):

<!---Set file upload destination path to nominationUploads/firstname_lastname--->
<cfset destination = expandPath("./nominationUploads/#fname#_#lname#/")>

<!---If the destination directory does not exist, create it.  This will be unique for each nominee.--->
<cfif not directoryExists(destination)>
    <cfdirectory action="create" directory="#destination#">
</cfif>

<!---Upload  document to the destination.  Accept only MSWord, PDF, RTF and plain text files.--->
<cffile action="upload"
        filefield="nominationLetter"
        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 clientNominationLetter = #file.ClientFile#>
<!---Create variable to reference renamed document.  Retain orignal file extension.--->
<cfset renameNomination = "nominationLetter"&"."&#cffile.clientFileExt#>

<!---Rename uploaded document using variable.  Save renamed document to original destination.--->
<cffile action="rename"
        source="#destination##File.ServerFile#"
        destination="#destination##Trim(renameNomination)#">


<!---Upload document to the destination.  Accept only MSWord, PDF, RTF and plain text files.--->
<cffile action="upload"
        filefield="curriculumVita"
        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 clientCurriculumVita = #file.ClientFile#>
<!---Create variable to reference renamed document.  Retain orignal file extension.--->
<cfset renameCurriculumVita = "curriculumVita"&"."&#cffile.clientFileExt#>

<!---Rename uploaded document using variable.  Save renamed document to original destination.--->
<cffile action="rename"
        source="#destination##File.ServerFile#"
        destination="#destination##Trim(renameCurriculumVita)#">

<!---Upload  document to the destination.  Accept only MSWord, PDF, RTF and plain text files.--->
<cffile action="upload"
        filefield="recommendation"
        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 clientRecommendation = #file.ClientFile#>
<!---Create variable to reference renamed document.  Retain orignal file extension.--->
<cfset renameRecommendation = "recommendation"&"."&#cffile.clientFileExt#>

<!---Rename uploaded document using variable.  Save renamed document to original destination.--->
<cffile action="rename"
        source="#destination##File.ServerFile#"
        destination="#destination##Trim(renameRecommendation)#">


    <cfif Len(form.recommendation2)>
        <!---Upload  document to the destination.  Accept only MSWord, PDF, RTF and plain text files.--->
        <cffile action="upload"
                filefield="recommendation2"
                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 clientRecommendation2 = #file.ClientFile#>
        <!---Create variable to reference renamed document.  Retain orignal file extension.--->
        <cfset renameRecommendation2 = "recommendation2"&"."&#cffile.clientFileExt#>

        <!---Rename uploaded document using variable.  Save renamed document to original destination.--->
        <cffile action="rename"
                source="#destination##File.ServerFile#"
                destination="#destination##Trim(renameRecommendation2)#">
    </cfif>

    <cfif Len(form.recommendation3)>
        <!---Upload  document to the destination.  Accept only MSWord, PDF, RTF and plain text files.--->
        <cffile action="upload"
                filefield="recommendation3"
                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 clientRecommendation3 = #file.ClientFile#>
        <!---Create variable to reference renamed document.  Retain orignal file extension.--->
        <cfset renameRecommendation3 = "recommendation3"&"."&#cffile.clientFileExt#>

        <!---Rename uploaded document using variable.  Save renamed document to original destination.--->
        <cffile action="rename"
                source="#destination##File.ServerFile#"
                destination="#destination##Trim(renameRecommendation3)#">
    </cfif>

    <cfif Len(form.recommendation4)>
        <!---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)#">
    </cfif>

    <cfif Len(form.recommendation5)>
        <!---Upload  document to the destination.  Accept only MSWord, PDF, RTF and plain text files.--->
        <cffile action="upload"
                filefield="recommendation5"
                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 clientRecommendation5 = #file.ClientFile#>
        <!---Create variable to reference renamed document.  Retain orignal file extension.--->
        <cfset renameRecommendation5 = "recommendation5"&"."&#cffile.clientFileExt#>

        <!---Rename uploaded document using variable.  Save renamed document to original destination.--->
        <cffile action="rename"
                source="#destination##File.ServerFile#"
                destination="#destination##Trim(renameRecommendation5)#">
    </cfif>

    <cfif Len(form.recommendation6)>
        <!---Upload  document to the destination.  Accept only MSWord, PDF, RTF and plain text files.--->
        <cffile action="upload"
                filefield="recommendation6"
                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 clientRecommendation6 = #file.ClientFile#>
        <!---Create variable to reference renamed document.  Retain orignal file extension.--->
        <cfset renameRecommendation6 = "recommendation6"&"."&#cffile.clientFileExt#>

        <!---Rename uploaded document using variable.  Save renamed document to original destination.--->
        <cffile action="rename"
                source="#destination##File.ServerFile#"
                destination="#destination##Trim(renameRecommendation6)#">
    </cfif>

</cfif>


Комментарии
1 ответ

Учитывая, что вы делаете это:

    <!---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");

Тогда если вы когда-нибудь понадобится, чтобы изменить способ загрузки файлов обрабатываются, вы можете просто изменить одну функцию, чтобы это произошло.

Хорошее правило: каждый раз, когда вы скопировать и вставить блок кода, а затем заменить одну или две строки, чтобы сделать его функция.

3
ответ дан 29 марта 2011 в 08:03 Источник Поделиться