This discussion is archived
3 Replies Latest reply: Jan 8, 2013 11:41 PM by Sudipto Desmukh RSS

Multiple file upload - multiple issue with af:inputFile

Tapas Bose Newbie
Currently Being Moderated
I am trying to create multiple file upload by using multiple af:inputFile component. Here is my xhtml:
<?xml version='1.0' encoding='UTF-8'?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1"
          xmlns:f="http://java.sun.com/jsf/core"
          xmlns:h="http://java.sun.com/jsf/html"
          xmlns:af="http://xmlns.oracle.com/adf/faces/rich">
  <jsp:directive.page contentType="text/html;charset=UTF-8"/>
  <f:view>
    <af:document id="d1" title="Home">
      <af:form id="f1" usesUpload="true">
        <af:commandButton text="Add" immediate="true"
                          actionListener="#{viewScope.massUpload.add}"
                          id="cb2"/>
        <af:panelGroupLayout layout="vertical" id="pgl1" partialTriggers="cb1">
          <af:iterator value="#{viewScope.massUpload.fileComponents}" var="fileComponent"
                       id="i1" varStatus="status">
            <af:inputFile binding="#{fileComponent}" id="if1"
                          value="#{viewScope.massUpload.files[status.index]}"/>
          </af:iterator>
          <af:commandButton text="Upload"
                            actionListener="#{viewScope.massUpload.upload}"
                            id="cb1"/>
        </af:panelGroupLayout>
      </af:form>
    </af:document>
  </f:view>
</jsp:root>
and this is the managed bean:
package com.edfx.massupload.bean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.event.ActionEvent;

import oracle.adf.view.rich.component.rich.input.RichInputFile;

import oracle.stellent.ridc.IdcClientException;

import org.apache.myfaces.trinidad.model.UploadedFile;


public class MassUploadBean {
    private List<RichInputFile> fileComponents = new ArrayList<RichInputFile>();
    private List<UploadedFile> files = new ArrayList<UploadedFile>();

    public MassUploadBean() {
        super();        
    }
    
    public void add(ActionEvent event) {   
        fileComponents.add(new RichInputFile());
        files.add(null);
    }
    
    public void upload(ActionEvent event) throws IdcClientException {
        for(UploadedFile file : files) {
            System.out.println(file.getFilename());
        }
    }

    public void setFiles(List<UploadedFile> files) {
        this.files = files;
    }

    public List<UploadedFile> getFiles() {
        return files;
    }

    public void setFileComponents(List<RichInputFile> fileComponents) {
        this.fileComponents = fileComponents;
    }

    public List<RichInputFile> getFileComponents() {
        return fileComponents;
    }
}
The problems that I am facing are:

1. I have clicked on the Add button; a new file upload component added to the page; I browse a file; Again clicked on the Add button; Another file upload component added and the first file upload get submitted (but it is having a border, or something similar); I browse file from the second file upload; Again click on Add button; now the first file upload get cleared (reset) and the second file upload get submitted;
2. Whenever I clicked on the Add button the file get uploaded. Why? How to resist it? I have tried by setting atoSubmit="false" but no luck. Also tried to put the iterator, file uploader and the Upload button inside af:subForm
3. My requirement is to Upload the file when the upload button is clicked, not before.

Am I doing something wrong? Any pointer would be very helpful.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points