Skip to main content
February 23, 2023
Question

How to get filename before submit during file upload?

  • February 23, 2023
  • 4 replies
  • 0 views

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

    4 replies

    harshitb6843
    February 23, 2023

    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. 

    mikes0011
    Brainy
    February 23, 2023

    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:

    The Expression Guru
    abhayd3663
    February 23, 2023

    [mention:b45a4f6a20b14a008e4e0ec732a8bf98:e9ed411860ed4f2ba0265705b8793d05]

    Interesting!!! Thanks for sharing.