File-upload with multiple files fails in Primefaces 5.1

后端 未结 1 1446
心在旅途
心在旅途 2020-12-21 06:45

I try to do a file upload in primefaces with PF 5.1 and OmniFaces version 1.7.

My .xhtml code:



        
相关标签:
1条回答
  • 2020-12-21 07:25

    p:fileUpload multiple="true" uploads each file in its own request. Problem is, the requests happen simultaneously, which in your case triggers conversation lock timeout, since each request tries to access the same conversation. Another problem is that JSF actually requires client-side code to send requests sequentially, and its server-side code is not thread-safe.

    I advise making the upload requests happen sequentially. There are multiple ways to achieve this.

    1. PrimeFaces 5.2.6 and above: you should be able to just use the sequential attribute:

      <p:fileUpload sequential="true" ...
      

      (The attribute was implemented in PF Issue #403.)

    2. This answer suggests a way via Javascript, by configuring the jQuery File Upload Plugin, which PrimeFaces uses.

    3. Add a Weld dependency. Then you could change the default lock timeout (in a non-portable CDI way), so the requests form a queue on the server. The default lock timeout is 1 second. The following code changes lock timeout globally for all conversations.

      import javax.enterprise.context.ConversationScoped;
      import javax.enterprise.context.Initialized;
      import javax.enterprise.event.Observes;
      import javax.inject.Inject;
      import javax.servlet.ServletRequest;
      import org.jboss.weld.context.http.HttpConversationContext;
      
      public class ConversationTimeoutDefaultSetter {
      
          @Inject
          private HttpConversationContext ctx;
      
          public void conversationInitialized(
                  @Observes @Initialized(ConversationScoped.class)
                  ServletRequest payload) {
              ctx.setConcurrentAccessTimeout(10000L); // 10 seconds
          }
      
      }
      

    Update: I forgot, that Initialized requires CDI 1.1, and you seem to be using 1.0. You can set the timeout when you're beginning your conversation.

    0 讨论(0)
提交回复
热议问题