Storing file name when uploading using Coldfusion

前端 未结 3 1757
遇见更好的自我
遇见更好的自我 2021-01-20 21:13

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

相关标签:
3条回答
  • 2021-01-20 21:30

    You can find the file name before saving.

    Railo:

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

    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/

    0 讨论(0)
  • 2021-01-20 21:33

    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>
    
    0 讨论(0)
  • 2021-01-20 21:39

    As lvmisooners said,

    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.

    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!

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

    0 讨论(0)
提交回复
热议问题