使用 Coldfusion 上传时存储文件名 [英] Storing file name when uploading using Coldfusion

查看:25
本文介绍了使用 Coldfusion 上传时存储文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to store the filename of the selected file to be uploaded into a hidden input field on the form. my form looks like this

<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>

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

This is then sent to the controller which writes it to the database how ever I cannot work out a way to get the name of the file to store in the "filename" field.

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

Does anyone have a solution on how you can populate a field with the name of the file that is selected to be uploaded?

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

I have added the CFFILE.serverFile in and it worked once, but I'm guessing thats because it grabbed the previously uploaded files name.

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

Now when loading the page I get Serverfile is undefined in CFFILE and so it does not let me populate the form with the files name.

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

My code looks like this now to try and work around it how ever this doesn't seem to work either.

<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>"/>

推荐答案

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

The filename does not become available until the file is uploaded. This happens after the form is posted. The only way around this is to try posting the fileupload via AJAX and then returning the filename.

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

Otherwise, you can assign the value to the field after the file is upload and the form is posted.

    <cfset form.filename = CFFILE.serverfile>

这篇关于使用 Coldfusion 上传时存储文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆