Thursday, September 5, 2013

How to upload and download file using ADF?


This blog explains about uploading and downloading any type of file as a BlobDomain data type into Database and retrieves in the same format.

- Ensure 'usesUpload=true'  in main .jspx page
- Develop a page/page fragment which accepts File as shown below

- Below is the code snippet for 'Download' button


- This is how screen looks like, User will upload file by clicking on 'Choose File', By clicking on 'Save' button Uploaded file will be saved into database

- Define below method in Page, and this will be invoked by clicking on 'Save Uploaded File' button on page.
       

     * This method invoked by clicking on 'SaveFile' Button,
     * Sets the uploaded file info to DTO and invoke App Module Impl to save into Database
     * @param actionEvent
     */
    public void saveUploadedFile(ActionEvent actionEvent) {
        BindingContainer bc = BindingContext.getCurrent().getCurrentBindingsEntry();
        OperationBinding ob = bc.getOperationBinding("saveUploadedFile");
        FileUtilDTO dto = new FileUtilDTO();
        dto.setContentType(contentType);
        dto.setFileContent(content);
        dto.setFileName(fileName);
        ob.getParamsMap().put("dto", dto);
        ob.execute();
    }
       
 

- This method converts uploaded File into BlobDomain data type and sets to class level variables.

       

      /**
     * This method invoked whenever user uploads file
     * @param valueChangeEvent
     */
    public void onFileUpload(ValueChangeEvent valueChangeEvent) {
        // The event give access to an Uploade dFile which contains data about the file and its content
        UploadedFile file = (UploadedFile)valueChangeEvent.getNewValue();
        // Get the original file name
        fileName = file.getFilename();
        // get the mime type
        contentType =
            file.getContentType(); 
        content = createBlobDomain(file);
    }
       
 

- This method converts UploadedFile into BlobDomain data type.
       

          /**
     * This method converts uploadedFile object
     * into BlobDomain datatype
     * @param file
     * @return
     */
    private BlobDomain createBlobDomain(UploadedFile file) {
        // init the internal variables
        InputStream in = null;
        BlobDomain blobDomain = null;
        OutputStream out = null;
        try {
            // Get the input stream representing the data from the client
            in = file.getInputStream();
            // create the BlobDomain datatype to store the data in the db
            blobDomain = new BlobDomain();
            // get the outputStream for hte BlobDomain
            out = blobDomain.getBinaryOutputStream();
            // copy the input stream into the output stream
            /*
             * IOUtils is a class from the Apache Commons IO Package (http://www.apache.org/)
             * Here version 2.0.1 is used
             * please download it directly from http://projects.apache.org/projects/commons_io.html
             */
            IOUtils.copy(in, out);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.fillInStackTrace();
        }

        // return the filled BlobDomain
        return blobDomain;
    }
       
 

- This method invoked by clicking on 'Download' button on <af:table> component for a selected Row, Gets the selected Row, corresponding BlobDomain and converts it into OutputStream based on selected file content type
       

            /**
     * This method prepares BlobDomain data to OutputStream
     * based on Content Type
     * @param facesContext
     * @param outputStream
     */
    public void downloadImage(FacesContext facesContext,
                              OutputStream outputStream) {
        UIXTable empTable = getFileContentTable();
        // Get the Selected Row Data
        FacesCtrlHierNodeBinding rowdata =
            (FacesCtrlHierNodeBinding)empTable.getRowData(empTable.getRowIndex());
        FileUploadDownloadROVORowImpl selectedRow =
            (FileUploadDownloadROVORowImpl)rowdata.getRow();
        // the value is a BlobDomain data type
        BlobDomain blob = selectedRow.getDocumentcontent();

        try {
            // copy hte data from the BlobDomain to the output stream
            IOUtils.copy(blob.getInputStream(), outputStream);
            // cloase the blob to release the recources
            blob.closeInputStream();
            // flush the outout stream
            outputStream.flush();
        } catch (IOException e) {
            // handle errors
            e.printStackTrace();
            FacesMessage msg =
                new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(),
                                 "");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }
       
 

This method defined in AMImpl class, Invoke whenever user  clicks on 'Save' button after uploading, This method persists the file related information into database.

       

               /**
     * This method creates new row and set the file related info
     * commits to database.
     * @param dto
     * @return
     */
    public boolean saveUploadedFile(FileUtilDTO dto){
        TRPRO_TEST_FILEUPLOADVOImpl vo = getTRPRO_TEST_FILEUPLOADVO1();
        TRPRO_TEST_FILEUPLOADVORowImpl row = (TRPRO_TEST_FILEUPLOADVORowImpl)vo.createRow();
        row.setContenttype(dto.getContentType());
        row.setDocumentcontent((BlobDomain)dto.getFileContent());
        row.setDocname(dto.getFileName());
        vo.insertRow(row);
        getDBTransaction().commit();
        return true;
    }
       
 




4 comments:

  1. hi, you can send this code for me le171282[at]gmail.com

    ReplyDelete
  2. hi, you can send this code for me daniel.cruz.garcia@gmail.com

    Please, thank you for your code.

    ReplyDelete
  3. hi, what is FileUtilDTO dto = new FileUtilDTO();
    i getting error there.

    ReplyDelete
  4. hi, you can send this code for me at manish.raj.sapkota@gmail.com

    ReplyDelete