Remove file immediately, if extension is not "PDF"

Certified Associate Developer

HI All,

I am using a!fileUploadField for uploads file, which has validated to allow only "PDF" file, 

Our ask is, if uploaded file extension is not PDF, shall remove immediately, how i can do that?

Note:- file upload feature i have given on a grid column and it's single select.

  Discussion posts and replies are publicly visible

Parents Reply Children
  • +1
    Certified Lead Developer
    in reply to nitin07

    Use the below code then. 

    a!fileUploadField(
                    label: "File Upload",
                    labelPosition: "ABOVE",
                    value: ri!document_doc,
                    saveInto: {
                      ri!document_doc,
                      a!save(
                        ri!document_doc,
                        if(
                          rule!TA_checkFileExtension(document_doc: ri!document_doc)="PDF",
                          null,
                          ri!document_doc
                        )
                      )
                    },
                    target: cons!DA_DESIGNER_DOCUMENTS,
                  )


    TA_checkFileExtension

    tostring(
      a!fileUploadField(value: ri!document_doc).contents.value.extension
    )

  • Be aware that accessing internal state of components like in TA_checkFileExtension is unsupported and can break without notice any time.

  • 0
    Certified Lead Developer
    in reply to Stefan Helzle

    This can be done by using the "content details" plug-in which can also access object properties by ID for freshly-uploaded files (though annoyingly the output is only plaintext so we must scrape the result).  I've switched to that method for the times when it's truly necessary.

    /* Parent Rule =  UTIL_getNewUploadDocName */
    a!localVariables(
      local!rawDetails: if(
        a!isNullOrEmpty(ri!doc),
        "",
        getcontentobjectdetailsbyid(ri!doc)
      ),
      local!wholeName: index(extract(
        local!rawDetails,
        "[Name: ",
        ", UUID:"
      ), 1, null()),
    
      rule!UTIL_processDocumentName(
        docName: local!wholeName
      )
    )

    /* Child Rule = UTIL_processDocumentName (uses RegEx plug-in) */
    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
      )
    )

    Result:

    cc: please consider adopting this method instead of the unsupported component-hacking that was formerly required for this functionality, at least in terms of what we recommend to people around here Slight smile