【问题标题】:uploading a file, renaming it and placing it in a particular location on server using Coldfusion使用 Coldfusion 上传文件、重命名并将其放置在服务器上的特定位置
【发布时间】:2018-11-13 11:15:47
【问题描述】:

用户从前端 HTML 表单提交文件,该表单包含部门、部门名称、部门编号、部门编号、年份、电子邮件、电话等字段。提交的文件可能具有用户名。但是,当它被上传时,我希望它被命名为Departmentname_departmentnumber_sectionnumber

所以,如果部门是Accounting,部门编号是123,部门是1,那么文件名就是Accounting_123_1.doc。扩展名将是提交的任何类型的文件(文本、MS-Word 的 .doc 或 .docx、PDF 或 RTF),用户只能上传扩展名为 .txt、.doc、.docx、pdf、rtf 的文件的附件。

另外,我希望它存储在服务器上的特定位置。因此,如果 Division 是 Corporate Finance 并且年份是 2011-2012,它应该存储在服务器上"E:\Files Submitted\2011-2012\Corporate Finance\""E:\Files Submitted\" 部分在目录名称中保持不变。

<cfset submittedfileName = #form.departmentname#&"_"&#form.departmentnumber#&"_"&#form.section_number_1#&"."&#cffile.ClientFileExt#>
<cfset filedirectoryYear = "E:\Files Submitted\"&#form.current_year#&"\"&#form.division#&"\">

<!--- ensure that the user uploads attachments of type with extension .txt, .doc, .docx, pdf, rtf only--->
<cfif FORM.attachment_1 neq "">          
  <cffile action="upload" 
    accept="text/plain,application/msword,application/pdf,application/rtf"        
    filefield="attachment_1"
    destination="E:\temp\uploads"
    nameconflict="Makeunique" 
  >

  <!--- rename the file and move it to permanent destination --->         
  <cffile action="rename" 
    source="E:\temp\uploads\#cffile.serverFileName#" 
    destination=#filedirectoryYear#&#submittedfileName#&#cffile.ClientFileExt#
  >

  <!--- now create a temporary variable for the attachment so that it can be emailed later on --->
  <cfset attachment_local_file_1 = #filedirectoryYear#&#submittedfileName#&#cffile.ClientFileExt#>            
</cfif>

我使用了cffile.ClientFileExt,因为文件在没有扩展名的情况下上传,但在destination=#filedirectoryYear#&amp;#submittedfileName#&amp;#cffile.ClientFileExt# 收到错误

"multiple items at this position: Missing Token > or />

我正在使用 Coldfusion 8。如果我在哪里出错以及如何解决它,我们将不胜感激。

【问题讨论】:

  • 仅供参考,不必在代码中的任何位置都将## 中的每个变量都括起来。这很好:&lt;cfset filedirectoryYear = "E:\Files Submitted\" &amp; form.current_year &amp; "\" &amp; form.division &amp; "\"&gt; 并且可读性更强。
  • 另外,不要依赖 mime 类型检查来确保安全 petefreitag.com/item/701.cfm

标签: coldfusion


【解决方案1】:

问题在于您的代码中的&amp;。试试这个

<cffile 
  action="rename" 
  source="E:\temp\uploads\#cffile.serverFileName#" 
  destination="#filedirectoryYear##submittedfileName##cffile.ClientFileExt#"
>

ColdFusion 中的字符串连接要么通过变量插值发生

<cfset foo = "FixedString_#variable#_FixedString">

或作为表达式

<cfset foo = "FixedString" & variable & "FixedString">

不要混淆两者。

【讨论】:

    猜你喜欢
    • 2016-10-09
    • 2012-01-08
    • 2011-09-13
    • 1970-01-01
    • 2013-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-08
    相关资源
    最近更新 更多