Forum Stats

  • 3,815,377 Users
  • 2,259,010 Discussions
  • 7,893,063 Comments

Discussions

Issue with File ContentType with <af:inputFile> tag

Using below tag - af:inputFile

<af:inputFile label="#{inventoryUIBundle.FILE}:" id="if1"

                                value="#{pageFlowScope.InventoryImportEditBean.inputFile}"/>the uploaded MIME ContetType for CSV File is shown as "application/vnd.ms-excel " when checked with Firefox.

For the Same file, MIMEContent Type is shown as "text\csv" when checked with Chrome \ IE

How to get same File ContentType irrespective of Browser?

Comments

  • Timo Hahn
    Timo Hahn Senior Principal Technical Consultant - Oracle ACE Director Member, Moderator Posts: 38,270 Red Diamond

    User, tell us your exact JDev version, please!

    You can't set the mime type when you upload a file. All you can do is to get the mime type and act on this.

    Code to get the mime type:

    String mime = uploadedFile.getContentType();
    

    or you use the file extension to determine the type.


    Timo

  • dvohra21
    dvohra21 Member Posts: 14,486 Gold Crown

    Specify ContentType in the af:fileDownloadActionListener.


    <af:commandLink text="Download" id="cl1" partialSubmit="true">
                            <af:fileDownloadActionListener filename="#{row.DocumentName}"
                                            contentType="#{row.DocumentType}"
                                            method="#{pageFlowScope.MultipleDocumentUpload.handleDownload}"/>
                          </af:commandLink>
    
  • Timo Hahn
    Timo Hahn Senior Principal Technical Consultant - Oracle ACE Director Member, Moderator Posts: 38,270 Red Diamond

    @dvohra21 the question is about af:inputFile, not about af:fileDownloadActionListener.


    Timo

  • dvohra21
    dvohra21 Member Posts: 14,486 Gold Crown

    The af:commandButton could be added within af:inputFile.

    <af:inputFile label="#{inventoryUIBundle.FILE}:" id="if1"
                                    value="#{pageFlowScope.InventoryImportEditBean.inputFile}">
    <af:commandButton text="Download" id="cl1" partialSubmit="true">
                            <af:fileDownloadActionListener filename="#{row.DocumentName}"
                                            contentType="#{row.DocumentType}"
                                            method="#{pageFlowScope.MultipleDocumentUpload.handleDownload}"/>
                          </af:commandButton>
    </af:inputFile>
    
  • Timo Hahn
    Timo Hahn Senior Principal Technical Consultant - Oracle ACE Director Member, Moderator Posts: 38,270 Red Diamond

    @dvohra21 have you tried this code? I don't think so. af:inputfile opens a dialog to upload a file. then your button adds an af:fileDownloadListener that is used to send a file to the client. What should this do?

  • dvohra21
    dvohra21 Member Posts: 14,486 Gold Crown
    edited May 6, 2022 5:02PM

    Thanks Timo. Upload and download together won't run.

    How to get same File ContentType irrespective of Browser?

    Use the https://www.appservgrid.com/documentation111/api/adffaces/docs/apidocs/oracle/adf/view/faces/model/UploadedFile.html#getContentType() method. Add a valueChangeListener to inputFile. In the backing bean get the content type.

      /**Method to Upload File ,called on ValueChangeEvent of inputFile
         * @param vce
         */
        public void uploadFileVCE(ValueChangeEvent vce) {
            if (vce.getNewValue() != null) {
                //Get File Object from VC Event
                UploadedFile fileVal = (UploadedFile) vce.getNewValue();
              
                System.out.println(fileVal.getContentType());
               
            }
    }
    


    The  download functionality could be added separately to a link in table column, and with an af:fileDownloadActionListener inside the link.