【问题标题】:Can file names be changed on uploading with cffile?使用 cffile 上传时可以更改文件名吗?
【发布时间】:2013-03-04 10:44:48
【问题描述】:

我目前正在尝试创建一个页面,乐队可以将他们自己的徽标上传到网站上,以便在需要的地方使用。目前,我已经创建了一些东西,允许用户将徽标上传/删除到分配的目录。但我想要做的不是创建一个 band_logo 字段,而是拥有它,以便乐队徽标文件名成为数据库中乐队的 id。由于这是独一无二的,这意味着我没有冗长的过程来创建一个字段来保存他们的徽标名称。我知道有一个cffile action="rename" 选项,但这是一个更冗长的处理过程。

【问题讨论】:

    标签: file-upload coldfusion upload cfml


    【解决方案1】:

    是的,如果您在destination 中提供文件名,它将在上传时重命名文件。

    <cffile action="upload" destination="/path/to/some/directory/#session.bandName#.jpg" ... />
    

    Renaming Files As They Are Uploaded (how CFFILE actually works)

    顺便说一句,在 Google 上搜索“name file on cffile upload”找到了那篇文章...

    但是,您仍然可能想要使用

    <cffile action = "upload" ...>
     then 
    <cffile action = "rename" ...>
    

    因为除非您在客户端获取文件扩展名并将其传递给服务器,否则如果他们不上传 .jpg 或您指定的任何扩展名,硬编码文件扩展名可能会导致问题。它没有那么多代码或开销,而且更安全。

    【讨论】:

      【解决方案2】:
      <cfif #ServerFileExt# EQ 'jpg'>
      <cffile action="upload" destination="/path/to/some/directory/#session.bandName#.jpg" />
      
      <cffile action="rename"
      source="path/to/some/directory/#session.bandName#.jpg"
      destination="path/to/some/directory/#getData.bandsID#.jpg">
      
      <cfelse>
      
         <cflocation addtoken="no" url="back to form upload page"> 
      
       </cfif>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-08-28
        • 2015-01-03
        • 1970-01-01
        • 2013-10-09
        • 1970-01-01
        • 2021-12-17
        • 1970-01-01
        相关资源
        最近更新 更多