【问题标题】:Storing file name when uploading using Coldfusion使用 Coldfusion 上传时存储文件名
【发布时间】:2013-01-03 16:34:16
【问题描述】:

我正在尝试将要上传的所选文件的文件名存储到表单上的隐藏输入字段中。我的表格是这样的

<form id="uploadattachment" enctype="multipart/form-data" 
       method="post" action="/governance/attachmentfilestore">

  <cfif isDefined("fileUpload")>
        <cffile action="upload"
                fileField="fileUpload"
                accept="application/pdf"
                nameconflict="makeunique"
                destination="#ExpandPath( '/files/governance/upr/' )#">


       <input type="hidden" name="filename" id="filename" value="">
       <input type="hidden" readonly id="uprUUID" name="uprUUID" 
               style="width: 400px" value="<cfoutput>#params.key#</cfoutput>"/>
       <input type="hidden" readonly id="status" name="status" 
               style="width: 400px" value="1"/>
       <input name="fileUpload" type="file" style="width: 200px;" />
       <button type="submit" name="action" 
               class="submitBtn primary rightSubmitBtnSpace">Upload</button>
</form>

然后将其发送到将其写入数据库的控制器,但是我无法找到一种方法来获取文件名以存储在“文件名”字段中。

有没有人可以解决如何使用选择要上传的文件的名称填充字段?

我已经添加了 CFFILE.serverFile 并且它工作过一次,但我猜那是因为它获取了之前上传的文件名。

现在加载页面时,我得到的 Serverfile 在 CFFILE 中未定义,因此它不允许我使用文件名填充表单。

我的代码现在看起来像这样,可以尝试解决它,但它似乎也不起作用。

<cfif isDefined("CFFILE.serverFile")>
    <cfset form.filename = CFFILE.serverFile>
<cfelse>
     <cfset form.filename = "null">
</cfif>
<input type="hidden" name="filename" id="filename" 
        value="<cfoutput>#CFFILE.serverFile#</cfoutput>"/>

【问题讨论】:

    标签: sql coldfusion upload filenames


    【解决方案1】:

    文件名在文件上传之前不可用。这发生在表单发布后。解决此问题的唯一方法是尝试通过 AJAX 发布文件上传,然后返回文件名。

    否则,您可以在上传文件并发布表单后将值分配给该字段。

        <cfset form.filename = CFFILE.serverfile>
    

    【讨论】:

    • 在上传后获取名称是我的偏好。它更简单,并且给定属性 nameconflict="makeunique",它保证了正确的答案。
    • 仅供参考,已删除 deprecated file scope
    • 它应该是&lt;input type="hidden" name="filename" id="filename" value="#cffile.serverFile#"&gt; 另外,请务必检查 cffile 的文档。 cffile 结构中有大量可用的值livedocs.adobe.com/coldfusion/8/htmldocs/…
    • @Webgod,如果您单击 Leigh 提供的链接并点击 action="upload" 链接或我提供的链接,您将看到 Note: The file prefix is deprecated, in favor of the cffile prefix. Do not use the file prefix in new applications. 我在另一篇文章中看到她试图帮助您。 CFFile.serverFile 好。 File.serverFile 不好 :)
    • 谢谢大家。我知道这一点,但在 CF8 世界里生活了这么久,我只是出于习惯打字。感谢您引导我正确。
    【解决方案2】:

    您可以在保存前找到文件名。

    雷洛:

    GetPageContext().formScope().getUploadResource("myFormField").getName()
    

    土坯:

    function getClientFileName(fieldName) {
    var tmpPartsArray = Form.getPartsArray();
    var clientFileName = "";
    
    if (IsDefined("tmpPartsArray")) {
        for (local.tmpPart in tmpPartsArray) {
            if (local.tmpPart.isFile() AND local.tmpPart.getName() EQ arguments.fieldName) {
                return local.tmpPart.getFileName();
                }
            }
        }
    
    return "";
    }
    

    来源:http://www.stillnetstudios.com/get-filename-before-calling-cffile/

    【讨论】:

      【解决方案3】:

      正如 lvmisooners 所说,

      GetPageContext().formScope().getUploadResource("myFormField").getName()
      

      适用于 Railo(和 Lucee),但我注意到一个有趣的问题:如果浏览器是 IE,则返回完整的源路径,包括文件名。另一方面,Firefox 和 Chrome 只返回文件名。

      对于我的应用程序,我需要完整路径,但无法找到浏览器是 FireFox 还是 Chrome。如果有人有任何想法,我将不胜感激!

      (抱歉没有回复 lvmisooners,但我没有声望点可以回复。)

      【讨论】:

        猜你喜欢
        • 2019-04-19
        • 1970-01-01
        • 1970-01-01
        • 2023-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-15
        • 1970-01-01
        相关资源
        最近更新 更多