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

查看:226
本文介绍了使用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.

现在加载页面时,我得到的服务器文件在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天全站免登陆