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
  • 0
    Certified Lead Developer
    in reply to nitin07

    But in that case, even if you leave it blank and try to move to the next page, you'll end up not storing any value in it. As the required validation will not trigger if the field is not visible on the screen. 

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

    that fine, if no document uploaded but issue only with to upload wrong file (other that PDF)

  • +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

  • +1
    Certified Lead Developer
    in reply to nitin07

    I would urge you figure out a way to disable moving between tabs/pages when the validation has an error, implementing a form-level validation component for visibility.  You could use the functionality Harshit and I suggest below to store into a local variable a flag that becomes "true" if any uploaded files have an invalid extension per your business rules.

    I strongly suggest AGAINST simply clearing out the "value" of the File Upload Field if the uploaded document's extension is wrong, because the user experience will be exceptionally poor (they will simply see their uploaded file vanish with no explanation).