10 Replies Latest reply on May 3, 2008 11:11 PM by Phil Wilson

    fileUpload - Works..- Clear all resets name size, leaves dat

    Phil Wilson Apprentice

      Hey Guys,

      I have one more question. I have the fileUpload working great now. Thanks to you guys on this site.

      fileUpload - Works..- Clear all resets name size but leaves data.

      The name and file size are cleared.

      The data or picture are left after clear.


      Thanks
      Phil


      =======================

      RF Verison

      April 25, 2008 snapshot

      richfaces-ui-3.2.1.snap.jar
      richfaces-impl-3.2.1-snap.jar
      richfaces-api-3.2.1-snap.jar

      =======================

      Bean

      =======================


      import java.io.IOException;
      import java.io.OutputStream;
      import java.util.ArrayList;
      import java.util.Map;

      import javax.faces.context.FacesContext;

      import org.richfaces.event.UploadEvent;
      import org.richfaces.model.UploadItem;

      /**
      * @author Ilya Shaikovsky
      *
      */
      public class FileUploadBean2{

      private ArrayList files = new ArrayList();
      private int uploadsAvailable = 5;
      private boolean autoUpload = false;
      public int getSize() {
      if (getFiles().size()>0){
      return getFiles().size();
      }else
      {
      return 0;
      }
      }

      public FileUploadBean2() {
      }

      public void paint(OutputStream stream, Object object) throws IOException {
      // stream.write(getFiles().get((Integer)object).getData());
      stream.write(((File)getFiles().get(((Integer)object).intValue())).getData());

      }
      public void listener(UploadEvent event) throws Exception{
      UploadItem item = event.getUploadItem();
      File file = new File();
      file.setLength(item.getData().length);
      file.setName(item.getFileName());
      file.setData(item.getData());
      files.add(file);
      uploadsAvailable--;
      }

      public String clearUploadData() {
      files.clear();
      File file = new File();
      setUploadsAvailable(5);

      // Map session = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
      // FileUploadBean2 fileUploadBean2 = (FileUploadBean2)session.get("fileUploadBean2");

      System.out.println(" " );
      System.out.println(" ???????? Clear out session var : fileUploadBean2 ");
      System.out.println(" " );

      // Map<String, Object> sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
      // FileUploadBean2 fileUploadBean2 = (FileUploadBean2) sessionMap.get("fileUploadBean2"); // Retrieves the session scoped bean.
      // sessionMap.remove("fileUploadBean2"); // Removes the session scoped bean.
      // sessionMap.put("fileUploadBean2", new FileUploadBean2()); // Recreates the session scoped bean.

      // FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("fileUploadBean2").

      // FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("fileUploadBean2");
      //
      // FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("fileUploadBean2", new FileUploadBean2());
      //
      // FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("FileUploadBean2");

      // FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("FileUploadBean2", new FileUploadBean2());


      System.out.println(" " );
      System.out.println(" ???????? done method : fileUploadBean2 ");
      System.out.println(" " );

      return null;
      }

      public ArrayList getFiles() {
      return files;
      }

      public void setFiles(ArrayList files) {
      this.files = files;
      }

      public int getUploadsAvailable() {
      return uploadsAvailable;
      }

      public void setUploadsAvailable(int uploadsAvailable) {
      this.uploadsAvailable = uploadsAvailable;
      }

      public boolean isAutoUpload() {
      return autoUpload;
      }

      public void setAutoUpload(boolean autoUpload) {
      this.autoUpload = autoUpload;
      }

      }

      ======================================

      JSP

      ======================================

      <h:panelGrid columns="2" columnClasses="top,top">
      <rich:fileUpload fileUploadListener="#{fileUploadBean2.listener}"
      maxFilesQuantity="#{fileUploadBean2.uploadsAvailable}"
      id="upload"
      immediateUpload="#{fileUploadBean2.autoUpload}"
      acceptedTypes="jpg, gif, png, bmp">
      <a4j:support event="onuploadcomplete" reRender="info" />
      </rich:fileUpload>
      <h:panelGroup id="info">
      <rich:panel bodyClass="info">
      <f:facet name="header">
      <h:outputText value="Uploaded Files Info" />
      </f:facet>
      <h:outputText value="No files currently uploaded"
      rendered="#{fileUploadBean2.size==0}" />
      <rich:dataGrid columns="1" value="#{fileUploadBean2.files}"
      var="file" rowKeyVar="row">
      <rich:panel>
      <h:panelGrid columns="2">
      <a4j:mediaOutput element="img" mimeType="image/jpeg"
      createContent="#{fileUploadBean2.paint}" value="#{row}"
      style="width:100px; height:100px;" />
      <h:panelGrid columns="2">
      <h:outputText value="File Name:" />
      <h:outputText value="#{file.name}" />
      <h:outputText value="File Length(bytes):" />
      <h:outputText value="#{file.length}" />
      </h:panelGrid>
      </h:panelGrid>
      </rich:panel>
      </rich:dataGrid>
      </rich:panel>
      <rich:spacer height="3"/>

      <a4j:commandButton action="#{fileUploadBean2.clearUploadData}"
      reRender="info, upload" value="Clear Uploaded Data"
      rendered="#{fileUploadBean2.size>0}" />
      </h:panelGroup>
      </h:panelGrid>


      ========================================
      faces-config
      ========================================

      <managed-bean>
      <managed-bean-name>fileUploadBean2</managed-bean-name>
      <managed-bean-class>com.testApp.java.utilFileUpload.FileUploadBean2</managed-bean-class>
      <managed-bean-scope>session</managed-bean-scope>
      </managed-bean>


      ======================================

      package com.testApp.java.utilFileUpload;

      public class File {

      private String Name;
      private long length;
      private byte[] data;
      public byte[] getData() {
      return data;
      }
      public void setData(byte[] data) {
      this.data = data;
      }
      public String getName() {
      return Name;
      }
      public void setName(String name) {
      Name = name;
      }
      public long getLength() {
      return length;
      }
      public void setLength(long length) {
      this.length = length;
      }
      }