jsfprimefaces

In file uploading via h:inputFile,How to solve/troubleshoot ,null being of part variable inside upload method?


In browser developer tools, I checked xhr tab in networking, the request sent as supposed.

while debugging: upload mehtod is invoked but uploadedFile is null

project has working files uploading via p:fileUpload, so I think filters troubles are passed.

here are:

bean:

import javax.servlet.http.Part;

import com.d2m.view.bean.LetterWFBean;

@Named
@RequestScoped
public class AudioFileUpload {
    private Part uploadedFile ;//+setter/getter
    @Inject
     private LetterBean letterBean;
    public void upload() throws IOException{
        byte[] blob = uploadedFile.getInputStream().readAllBytes();
        letterBean.setAudioNote(blob);
    }
 ...
}

facelet:

<h:form id="hiddenAudioUploadForm" enctype="multipart/form-data" styleClass="ui-helper-hidden">
           <h:inputFile id="audioFile" value="#{audioFileUpload.uploadedFile}" />
           <p:remoteCommand name="hiddenAudioUploadCommand" action="#{audioFileUpload.upload}" process="@form" update="@none" />                                      
    </h:form>
        ...
    <p:button id="uploadAudio_CL" onclick="uploadAudioFile();return false;" icon="fa fa-check" />

JS:

function addAudioFileToUploadFiles(){
    const dataTransfer = new DataTransfer(); 
    dataTransfer.items.add(new File([ base64AudioBlob ], "a.ogg", { type: "audio/webm" }));
    document.getElementById("hiddenAudioUploadForm:audioFile").files = dataTransfer.files;
}
async function uploadAudioFile(){
    addAudioFileToUploadFiles();
    hiddenAudioUploadCommand();
}

Thanks-at least for reading!


Solution

  • troubleshooting stuff:

    for me:
    Cumulative approach helped:
    try with simplest 'should work' approach, and cumulate!


          <filter-name>PrimeFaces FileUpload Filter</filter-name>
          <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
          <init-param>
              <param-name>thresholdSize</param-name>
              <param-value>2097152</param-value>
          </init-param>
          <init-param>
              <param-name>uploadDirectory</param-name>
              <param-value>${primefaces.file.upload.directory}</param-value>
          </init-param>
      </filter>
      <filter-mapping>
          <filter-name>PrimeFaces FileUpload Filter</filter-name>
          <servlet-name>Faces Servlet</servlet-name>
          <dispatcher>REQUEST</dispatcher>
          <dispatcher>FORWARD</dispatcher>
      </filter-mapping>
    

    by commenting them, live comebacks to h:inputFile and every thing done as expected of code in question!