使用Coldfusion上传时存储文件名

时间:2021-02-12 20:25:50

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.

然后,它被发送给控制器,控制器将它写到数据库中,无论如何,我都无法找到一种方法将文件的名称存储到“filename”字段中。

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

有人知道如何用要上载的文件的名称填充字段吗?

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.

现在,当加载页面时,我得到的Serverfile在CFFILE中没有定义,所以它不允许我用文件名填充表单。

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

3 个解决方案

#1


5  

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.

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

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

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

    <cfset form.filename = CFFILE.serverfile>

#2


0  

You can find the file name before saving.

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

Railo:

Railo:

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

Adobe:

Adobe:

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 "";
}

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

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

#3


0  

As lvmisooners said,

lvmisooners说过,

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

works for Railo (and Lucee) but I noticed an interesting wrinkle: if the browser is IE than this returns the full source path including the filename. Firefox and Chrome on the other hand, return only the filename.

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

For my application I need the full path, but haven't been able to find that if the browser is FireFox or Chrome. If anyone has any ideas I would be most grateful!

对于我的应用程序,我需要完整的路径,但是还没有找到浏览器是FireFox还是Chrome。如果有人有什么想法,我将非常感激!

(Sorry for not replying to lvmisooners but I don't have the reputation points to reply.)

(很抱歉没有回复网友,但我没有可以回复的积分。)

#1


5  

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.

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

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

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

    <cfset form.filename = CFFILE.serverfile>

#2


0  

You can find the file name before saving.

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

Railo:

Railo:

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

Adobe:

Adobe:

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 "";
}

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

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

#3


0  

As lvmisooners said,

lvmisooners说过,

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

works for Railo (and Lucee) but I noticed an interesting wrinkle: if the browser is IE than this returns the full source path including the filename. Firefox and Chrome on the other hand, return only the filename.

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

For my application I need the full path, but haven't been able to find that if the browser is FireFox or Chrome. If anyone has any ideas I would be most grateful!

对于我的应用程序,我需要完整的路径,但是还没有找到浏览器是FireFox还是Chrome。如果有人有什么想法,我将非常感激!

(Sorry for not replying to lvmisooners but I don't have the reputation points to reply.)

(很抱歉没有回复网友,但我没有可以回复的积分。)