title: JSF Primefaces pass parameter to fileupload
body: <p>JSF 2.2 - Primefaces 4.0 - JBoss Wildfly</p>
  
  <p>Hi, I am trying to pass a parameter value to the file upload handler.
  I tried a few suggestions I found, but can't get it to work. </p>
  
  <p>I stepped a few steps back</p>
  
  <p>I need the #{fileUploadController.newItemId} in the controller</p>
  
  <p>When going to the file update page it is done with i.e. this URL 
  /fileUpload.jsf?newItemId=10</p>
  
  <p>I tried what it mentioned in 
  <a href="https://stackoverflow.com/questions/17233001/how-to-send-parameter-to-fileuploadlistener-in-primefaces-fileupload">How to send parameter to fileUploadListener in PrimeFaces fileUpload</a>
  and
  <a href="https://stackoverflow.com/questions/16836294/passing-value-to-the-backing-bean-with-primefaces-file-upload">Passing value to the backing bean with PrimeFaces file upload</a></p>
  
  <p>fileUpload.xhtml</p>
  
  <pre><code> &lt;f:metadata&gt;
                  &lt;f:viewParam name="newItemId"
                      value="#{fileUploadController.newItemId}" /&gt;
                  &lt;f:viewAction action="#{fileUploadController.loadData()}" /&gt;
              &lt;/f:metadata&gt;
              &lt;h:form id="item" enctype="multipart/form-data"&gt;
                  &lt;p:messages id="messages" /&gt;
  
                  &lt;p:panelGrid styleClass="panelGridCenter"&gt;
                      &lt;p:row&gt;
                          &lt;p:column&gt;
                              &lt;p:fileUpload
                                  fileUploadListener="#{fileUploadController.handleFileUpload}"
                                  validator="#{fileUploadController.validateFile}" mode="advanced"
                                  dragDropSupport="false" update="messages" sizeLimit="1000000"
                                  fileLimit="100" allowTypes="/(\.|\/)(gif|jpe?g|png)$/"&gt;
  
                              &lt;/p:fileUpload&gt;
                          &lt;/p:column&gt;
                      &lt;/p:row&gt;
                  &lt;/p:panelGrid&gt;
  
              &lt;/h:form&gt;
  </code></pre>
  
  <p>FileUploadController.java</p>
  
  <pre><code>public void handleFileUpload(FileUploadEvent event) {
          try {
  
              // Need item id here :) 
  
              UploadedFile file = event.getFile();
              InputStream inputStream = file.getInputstream();
              FileOutputStream outputStream = new FileOutputStream(file.getFileName());
              byte[] buffer = new byte[4096];
              int bytesRead = 0;
              while (true) {
                  bytesRead = inputStream.read(buffer);
                  if (bytesRead &gt; 0) {
                      outputStream.write(buffer, 0, bytesRead);
                  } else {
                      break;
                  }
              }
              outputStream.close();
              inputStream.close();
              Long imageId = serviceSLSB.saveImage(itemId, file, buffer);
              FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded and saved with id." + imageId);
              FacesContext.getCurrentInstance().addMessage(null, msg);
          } catch (Exception e) {
              String errorMessage = getRootErrorMessage(e);
              FacesMessage m = new FacesMessage(FacesMessage.SEVERITY_ERROR, errorMessage, "Saving unsuccessful");
              facesContext.addMessage(null, m);
          } 
      }
  
  public void loadData() {
      if (newItemId == null) {
          String message = "Bad request. Please use a link from within the system.";
          FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, message, null));
          return;
      } else {
          String message = "Your are adding images to item with id : " + newItemId;
          FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, message, null));
          return;
      }
  }
  </code></pre>