4 Replies Latest reply on Apr 14, 2010 11:47 PM by Steve Jagels

    file uploader

    Steve Jagels Newbie
      I am trying to add a file uploader control to a page.  I've changed the form tag to:

      <h:form enctype="multipart/form-data" >

      and I've added this tag:

      <s:fileUpload id="file" data="#{searchAction.search.data}" fileName="#{searchAction.search.fileName}" />

      There is a submit button:

      <h:commandButton action="#{searchAction.searchIt}" id="btnSearch" value="Search" styleClass="button primaryAction searchButton"/>

      It used to go through searchIt() when you clicked the submit button, but now it does not.  Any idea why?

      in components.xml I've added:

      xmlns:web="http://jboss.com/products/seam/web"

      http://jboss.com/products/seam/web http://jboss.com/products/seam/web-2.0.xsd

      <web:multipart-filter create-temp-files="true"
                            max-request-size="1000000"
                            url-pattern="*.seam" />

      in web.xml I have added:

      <filter>
            <filter-name>Seam Filter</filter-name>
            <filter-class>org.jboss.seam.web.SeamFilter</filter-class>
          </filter>
         
          <filter-mapping>
            <filter-name>Seam Filter</filter-name>
            <url-pattern>/*</url-pattern>
          </filter-mapping>     
        • 1. Re: file uploader
          alex becker Newbie

          Did you check the Console after serverstart? Maybe there is an error on startup?
          If not, can you clear the console before you click the button, then click the button and post the output please.
          Helpful are some more informations too (source of xhtml, searchAction..)
          alex

          • 2. Re: file uploader
            Steve Jagels Newbie
            There were no errors in the console after server start, and nothing shows up in the console when clicking submit.

            Here is the search.xhtml page (I've temporarily removed search parameters and search results while I try to get the file uploader to work):

            <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

            <ui:composition xmlns="http://www.w3.org/1999/xhtml"
                 xmlns:s="http://jboss.com/products/seam/taglib"
                 xmlns:ui="http://java.sun.com/jsf/facelets"
                 xmlns:f="http://java.sun.com/jsf/core"
                 xmlns:h="http://java.sun.com/jsf/html"
                 xmlns:rich="http://richfaces.org/rich"
                 xmlns:a4j="http://richfaces.org/a4j"
                 template="layout/template.xhtml">

            <ui:param name="page" value="search" />
            <ui:define name="body">
            <h:form enctype="multipart/form-data" >

            <s:fileUpload id="file" data="#{searchAction.search.data}" fileName="#{searchAction.search.fileName}" />

            <h1>Search</h1>

            <s:decorate id="errorMsg" >
                 <h:messages showDetail="true" showSummary="false" styleClass="alert"/>
            </s:decorate>

            <fieldset class="columns onecol">
                 <legend>Search Criteria</legend>
                 <s:decorate template="/layout/edit.xhtml" id="memberIdField" >
                      <ui:define name="reqd" ><strong class="required">*</strong></ui:define><ui:define name="label" >Member ID</ui:define>
                 </s:decorate>
                 <p class="actions" style="border-top: 1px solid #ffffff; margin: 0; padding: 0;">
                 <h:commandButton action="#{searchAction.searchIt}" id="btnSearch" value="Search" styleClass="button primaryAction searchButton"/>
            </p>
            </fieldset>

            </h:form>

            </ui:define>
            </ui:composition>



            SearchAction.java:

            package com.mbh.esln.sejClient.domain.session;

            import java.util.List;

            import javax.faces.context.ExternalContext;
            import javax.faces.context.FacesContext;

            import org.jboss.seam.ScopeType;
            import org.jboss.seam.annotations.In;
            import org.jboss.seam.annotations.Name;
            import org.jboss.seam.annotations.Out;
            import org.jboss.seam.annotations.Scope;
            import org.jboss.seam.annotations.web.RequestParameter;
            import org.jboss.seam.contexts.Contexts;
            import org.jboss.seam.international.StatusMessages;

            import javax.persistence.EntityManager;
            import javax.servlet.ServletOutputStream;
            import javax.servlet.http.HttpServletResponse;

            import com.mbh.esln.sejClient.domain.Search;
            import com.mbh.esln.sejClient.sqlmaps.Constants;

            @Name("searchAction")
            @Scope(ScopeType.SESSION)
            public class SearchAction {
                 
                 private Search search = new Search();
                 private List<Search> searchList = null;

                 @In(required=false) @Out(scope=ScopeType.SESSION, required=false)
                 private String memberIdSearch;
                 
                 @In(required=false) @Out(scope=ScopeType.SESSION, required=false)
                 private byte[] theFile;
                 
                 private int time1;

                 @Out(scope=ScopeType.SESSION, required=false)
                 private String fileName;

                 @Out(scope=ScopeType.SESSION, required=false)
                 private byte[] data;

                 @In
                 private EntityManager entityManager;
                 
                 @RequestParameter
                 private Long attachmentId;
                 
                 @In(value="#{facesContext.externalContext}")
                 private ExternalContext extCtx;
                 
                 @In String clientToken;

                @In
                 StatusMessages statusMessages;
                 
                 public void leaveApp(){
                      String url = Constants.CLAIMS_APPS_URL;
                      try {
                           FacesContext facesContext = FacesContext.getCurrentInstance();
                           ExternalContext externalCtx = facesContext.getExternalContext();
                           externalCtx.redirect(url);
                      } catch (Exception e) {
                           e.printStackTrace();
                      }
                 }

                 @SuppressWarnings("unchecked")
                 public void searchIt(){
                      System.out.println("you are now in searchIt().");     //not getting here at all
                      
                      Search attachment = entityManager.find(Search.class, attachmentId);
                      HttpServletResponse response = (HttpServletResponse)extCtx.getResponse();
            //          response.setContentType(attachment.getContentType());
                      response.addHeader("Content-disposition", "attachment; filename=\"" + attachment.getFileName() +"\"");
                      try {
                           ServletOutputStream os = response.getOutputStream();
                           System.out.println("getData: " + attachment.getData());
                           os.write(attachment.getData());
                           os.flush();
                           os.close();
                           FacesContext facesContext = FacesContext.getCurrentInstance();
                           facesContext.responseComplete();
                      } catch(Exception e) {
                           e.printStackTrace();
                      }
                 }
                 
                 private boolean isValidMemberId(String value) {
                      if (null == value) {
                           return false;
                      }
                      if(value.length() != 13){
                           return false;
                      }
                      for(int i = 0; i < 13; i++){
                           String myChar = value.substring(i, i+1);
                           if(!myChar.equals("0") && !myChar.equals("1") && !myChar.equals("2") && !myChar.equals("3") && !myChar.equals("4") && !myChar.equals("5") && !myChar.equals("6") && !myChar.equals("7") && !myChar.equals("8") && !myChar.equals("9")){
                                return false;
                           }
                      }
                      return true;
                 }

                 public String closeSession(String myPage){
            //          search = new Search();
            //          searchList = null;
                      Contexts.getSessionContext().remove("accum");
                      Contexts.getSessionContext().remove("addOrEdit");
                      Contexts.getSessionContext().remove("beginAccum");
                      Contexts.getSessionContext().remove("member");
                      return "/" + myPage + ".xhtml";
                 }
                 
                 public List<Search> getSearchList(){
                      return searchList;
                 }
                 
                 public Search getSearch() {
                      return search;
                 }

                 public void setSearch(Search search) {
                      this.search = search;
                 }

                 public String getMemberIdSearch() {
                      return memberIdSearch;
                 }

                 public void setMemberIdSearch(String memberIdSearch) {
                      this.memberIdSearch = memberIdSearch;
                 }

                 public void setSearchList(List<Search> searchList) {
                      this.searchList = searchList;
                 }

                 public int getTime1() {
                      return time1;
                 }

                 public void setTime1(int time1) {
                      this.time1 = time1;
                 }

                 public byte[] getTheFile() {
                      return theFile;
                 }

                 public void setTheFile(byte[] theFile) {
                      this.theFile = theFile;
                 }

                 public byte[] getData() {
                      return data;
                 }

                 public void setData(byte[] data) {
                      this.data = data;
                 }

                 public String getFileName() {
                      return fileName;
                 }

                 public void setFileName(String fileName) {
                      this.fileName = fileName;
                 }

                 public EntityManager getEntityManager() {
                      return entityManager;
                 }

                 public void setEntityManager(EntityManager entityManager) {
                      this.entityManager = entityManager;
                 }

                 public Long getAttachmentId() {
                      return attachmentId;
                 }

                 public void setAttachmentId(Long attachmentId) {
                      this.attachmentId = attachmentId;
                 }

                 public ExternalContext getExtCtx() {
                      return extCtx;
                 }

                 public void setExtCtx(ExternalContext extCtx) {
                      this.extCtx = extCtx;
                 }
            }
            • 3. Re: file uploader
              Gabor Jakab Newbie

              I would check if that search.data available where you trying to put your file data + also try to use the contentType attribute e.g.:


               <s:fileUpload 
                  id="image" data="#{yourHome.yourImage}" 
                  accept="image/png,image/gif,image/jpeg" 
                  contentType="#{yourHome.yourImageContentType}"/> 

              • 4. Re: file uploader
                Steve Jagels Newbie
                the <s:fileUpload> tag does not appear to have any effect on it, because if I remove it completely, and only have the enctype attribute added to the form tag, it still does not go through the searchIt().  So there is something about the enctype attribute that is causing this.