How to get filename before submit during file upload?

Certified Associate Developer

My rule input is of type document of CDT to upload excel file into the database.

  Discussion posts and replies are publicly visible

  • There is a dirty way of achieving it. People in the community do not recommend it but I have been using it for quite a long but in a way that if it ever stops working, I can just change the logic in the single expression rule to avoid errors. 

    tostring(
      a!fileUploadField(value: ri!doc).contents.value.filename
    )


    For this to work, just create this in an expression rule and create the 'doc' ri should be of type document. 

  • 0
    Certified Lead Developer
    in reply to Harshit Bumb (Appyzie)

    I eventually figured out a semi-workaround that doesn't require our old trick of "peeking at the back-end code of file upload field", for what it's worth...  Note: this uses the Content Tools plug-in (which most projects hopefully already have installed).

    a!localVariables(
      local!rawDetails: if(
        a!isNullOrEmpty(ri!doc),
        "",
        getcontentobjectdetailsbyid(ri!doc)
      ),
      
      index(extract(
        local!rawDetails,
        "[Name: ",
        ", UUID:"
      ), 1, null())
    )

    Note this returns the entire filename including extension of the uploaded document.  In my use case I really wanted the name and extension separately for processing reasons, so I implemented a further rule that the name could be passed into and the name / extension are returned as dictionary properties.  (Uses the RegEx plug-in)

    /* GLBL_UTIL_processDocumentName() */
    
    a!localVariables(
      local!hasNoExtension: search(".", ri!docName) = 0,
      
      local!namePart: if(
        local!hasNoExtension,
        ri!docName,
        regexsearch(
          pattern: ".*(?=\.)",
          searchString: ri!docName,
          regexFlags: "i"
        )[1].match
      ),
      
      local!extension: if(
        local!hasNoExtension,
        "",
        regexsearch(
          pattern: "[^\.]*$",
          searchString: ri!docName,
          regexFlags: "i"
        )[1].match
      ),
      
      a!map(
        name: local!namePart,
        extension: local!extension
      )
    )

    Combined, the original "get new uploaded doc name" rule in my system really ends up looking like this:

    a!localVariables(
      local!rawDetails: if(
        a!isNullOrEmpty(ri!doc),
        "",
        getcontentobjectdetailsbyid(ri!doc)
      ),
      local!wholeName: index(extract(
        local!rawDetails,
        "[Name: ",
        ", UUID:"
      ), 1, null()),
    
      rule!GLBL_UTIL_processDocumentName(
        docName: local!wholeName
      )
    )

    ...which gives this nice output:

  • 0
    Certified Lead Developer
    in reply to Harshit Bumb (Appyzie)

    BTW, if this is helpful to you, you can also use the same rule in the Content Tools plug-in to craft an expression rule that looks at a Doc Id and tells you whether or not it's newly-uploaded.  I've found this highly useful in on-form logic determining whether certain buttons or links are enabled, in efforts to reduce cluttering the filesystem with orphaned documents.