Follow by Email

Monday, 16 January 2017

Uploading mulitiple files to server path in Oracle ADF using af:inputFile



Hello All :)

Previously I have posted about uploading and downloading files from server path (Refer this post for more clarification), In that post I have described the process of uploading single file at a time but sometimes we need to upload multiple files at a time and for that requirement we can use same code with little modification


So In this post we'll see that how can we upload multiple files at a time using af:inputFile, Here I am using same sample application that is used in previous post



Default inputFile source looks like this

<af:inputFile label="Select" id="if1" valueChangeListener="#{viewScope.FileUploadDownloadBean.uploadFileVCE}"
                                          autoSubmit="true"/>



To enable multiple files selection in af:inputFile we have to set some properties


<af:inputFile label="Select" id="if1"
valueChangeListener="#{viewScope.FileUploadDownloadBean.uploadFileVCE}"
autoSubmit="true" rows="5" maximumFiles="5"/>

and what rows and maximumFiles properties means ?

rowsintYesan attribute that determines the number of rows in the file list. There will be a scrollbar which will be displayed when the number of files exceeds this number.

maximumFilesintYesan attribute that is used to restrict the number of files a user can upload. If the user tries to upload more files than specified in maximumFiles then an error is returned. If maximumFiles is less than 1 then the number of files is unlimited.

After setting these properties inputFile looks like this


Now UI part is done , next is code part so to get and upload multiples files from client we have to use a List data structure and then in value change listener we'll traverse that List and upload all files to server path.

ValueChangeListener of af:inputFile- 



    /*****Generic Method to Get BindingContainer**/
    public BindingContainer getBindingsCont() {
        return BindingContext.getCurrent().getCurrentBindingsEntry();
    }

    /**
     * Generic Method to execute operation
     * */
    public OperationBinding executeOperation(String operation) {
        OperationBinding createParam = getBindingsCont().getOperationBinding(operation);
        return createParam;

    }

    /**Method to Upload Multiple Files ,called on ValueChangeEvent of inputFile
     * @param vce
     */
    public void uploadFileVCE(ValueChangeEvent vce) {
        if (vce.getNewValue() != null) {
            //Get File Object from VC Event
            List<UploadedFile> lf = (List<UploadedFile>) vce.getNewValue();

            //Traverse over file list to upload all files
            for (UploadedFile fileVal : lf) {
                //Method to check if this file is uploaded previously or not
                OperationBinding duplOb = executeOperation("checkDuplicateFile");
                duplOb.getParamsMap().put("fileNm", fileVal.getFilename());
                duplOb.execute();
                if (duplOb.getResult() != null && "Y".equalsIgnoreCase(duplOb.getResult().toString())) {

                    System.out.println("Called");
                    //Upload File to path- Return actual server path
                    String path = uploadFile(fileVal);
                    System.out.println(fileVal.getContentType());
                    //Method to insert data in table to keep track of uploaded files
                    OperationBinding ob = executeOperation("setFileData");
                    ob.getParamsMap().put("name", fileVal.getFilename());
                    ob.getParamsMap().put("path", path);
                    ob.getParamsMap().put("contTyp", fileVal.getContentType());
                    ob.execute();

                }
            }
            // Reset inputFile component after upload
            ResetUtils.reset(vce.getComponent());
        }
    }

AMImpl Method to check duplicate file-


    /**Method to check for duplicate files
     * @param fileNm
     * @return
     */
    public String checkDuplicateFile(String fileNm) {
        ViewObject fileVo = this.getFileUpdDwn1();
        Row duplFile[] = fileVo.getFilteredRows("FileName", fileNm);
        if (duplFile.length > 0) {
            return "N";
        } else {
            return "Y";
        }
    }

For other methods and DB table refer previous post Uploading and downloading files from absolute server path
So All done for multiple files upload now run and check application



Sample ADF Application (Jdeveloper 12.1.3)- Download
Cheers :)  Happy Learning

28 comments :

  1. Hi Sir,

    I have a concern irrespective to this blog. Actually when i run my application on integrated weblogic in jdeveloper 12c it got deployed(run) successfully but when 2nd time when i run the same application rather then redeploying, it got undeployed. Am facing this issue on alternate basis everytime and on every machine.

    Thanks in advance.

    ReplyDelete
    Replies
    1. Hi Rahul

      Clean and rebuild application , delete .data folder in application directory , delete application from drs folder and check again

      Ashish

      Delete
  2. nice work Ashish
    But i have a small problem
    when i upload *.txt or *.csv or * .jpg ...etc. i can see the file content in inlineFrame without any problems, but when i upload *.docx or *.xlsx and click on the file to see it's content i get download operation with Zip file and this zip file has the same name of the servlet

    ReplyDelete
    Replies
    1. Hi Mariam

      We can not directly show docx and XLS file in web page , A plugin is required to show these types and I have never implemented this
      So you can put a question in OTN Forum

      Ashish

      Delete
  3. and when i try to download *.xlsx or *.docx the download process is working fine but i get another download file with the same name of servlet also

    ReplyDelete
    Replies
    1. That's because of inline frame , set render false for inline frame in case of file type is docx or xls

      Delete
  4. thanks ashish
    but I have another problem
    when I apply this post in Jspx page it was working fine
    but when I try to apply it into jsff page(dynamic region with task flow)
    I got this error

    "#{bindings.dynamicRegion1.regionModel}" (that was specified for the RegionModel "value" attribute of the region component with id "r1") evaluated to null.
    This is typically due to an error in the configuration of the objects referenced by this expression.
    If it helps, the expression "#{bindings.dynamicRegion1}" evaluates to "null".
    If it helps, the expression "#{bindings}" evaluates to "null". Now using an empty RegionMode

    ReplyDelete
    Replies
    1. Hi Mariam

      So you have used this code in jsff page and dropped that BTF as dynamic region in a jspx page ?
      Once check that definition for region in available in pagedef or not ?

      Ashish

      Delete
  5. Hi Mariam , be sure that Jsp page property ( usesupload = true)

    ReplyDelete
  6. I like you application it's vary good

    ReplyDelete
  7. hi Sir,

    I would like to save to DB using a button, not as the above, would you please advise how to separate above code

    //Method to insert data in table to keep track of uploaded files
    OperationBinding ob = executeOperation("setFileData");
    ob.getParamsMap().put("name", fileVal.getFilename());
    ob.getParamsMap().put("path", path);
    ob.getParamsMap().put("contTyp", fileVal.getContentType());

    thank you :)

    ReplyDelete
    Replies
    1. You need to call same on button click and to get af:inputFile value on button you need to bind it to bean using a variable, You can check this post
      Uploading and showing image file from absolute server path -Orace ADF

      Here you'll see that a variable is used to get file value

      Ashish

      Delete
  8. Hi Ashish,

    Getting 404 while downloading the sample from : https://drive.google.com/uc?export=download&id=0B0Usl2n1Wz8vNTJJTVZLUENIMmM.
    Could you provide a working url?

    Thanks,
    Sapna

    ReplyDelete
    Replies
    1. Checked link is working for me, Once check again please :)

      Delete
    2. It doesn't work. Getting error 404

      Delete
  9. Can you please update the download link? Thank you in advance!

    ReplyDelete
    Replies
    1. Yes I checked there is some problem with Google policies
      You can share your email id so that i can send you the sample app

      Ashish

      Delete
    2. Maybe upload to wetransfer and send the link, if google blocks java code as malicious(it happens sometimes)

      Delete
    3. You are welcome TheDarkSideOfSugar :)

      Delete
  10. Hi Ashish,

    I have referred your sample for multiple file upload,
    When I am uploading 11 files its sucessfull
    When I am uploading 25 + files it is throwing 503 error(A connection to server has failed : Status 503)
    in the log file I am seeing some multithreading issue.
    Can you help in this regard, how to escape from 503 error.

    Regards,
    Shikha

    ReplyDelete
  11. Hi,
    Can I please receive the download link?
    Thanks,
    Bogdan.

    ReplyDelete
  12. this is a great work Ashish , so how can save all this photos in database in same time and every photo in different row and different id

    ReplyDelete
  13. Hi Ashish.. please you can send me a email with the project to alarconignaciojuan@gmail.com?
    The download link is broken.
    Regards

    ReplyDelete
    Replies
    1. Juani

      Check the link again, I have fixed that

      Ashish

      Delete