0

I have a problem due to server restrictions. The server allows only data under 25MB to be transmitted. The data that is supposed to be uploaded is 80MB+. I'm working with JSF, Primefaces and JavaEE. My idea was to send the data in small packages via AJAX requests. The data is split into small enough chunks, but when I click the commandButton that is supposed to execute the AJAX as often as there are chunks, it sends the first data chunk with the correct content and immediately after js clicks the button and then it only sends the content of the last chunk in every request and just after the loop is finished completely. So there is a long break between the first request and the second. I don't need them to be in order. I just want all data to arrive in the backend.

My html looks like:

 <h:form> <input type="text" id="dataInput" name ="dataInput"> <h:commandButton action="#{ImportController.getTransmittedData()" id="button"> <p:ajax execute="dataInput"></p:ajax> </h:commandButton> </h:form> 

And my js looks like:

 startTransmittingData(){ for(let i=0; i<chunks.length(); i++){ document.getElementById("dataInput").value=chunks[i]; document.getElementById("button").click(); } } 

The ImportController only takes the data.

Is it just not possible to loop over a commandButton and get an instant reaction with AJAX? It alsow seems as if it's 'stuck' somewhere. When the loop is done it starts sending the AJAX requests with the wrong content, since the input changed while looping over the chunk-List.

I tried changing the ajax but nothing made a difference.

Edit: To make the order in which the code is executed more clear:

 chunks=[0,1,2,3]; i=0 button is clicked in loop and first chunk gets transmitted to ImportController. `i=1` the codeline where the button is clicked in js is called but the function ImportController is not called. `i=2` the codeline where the button is clicked in js is called but the function ImportController is not called. `i=3` the codeline where the button is clicked in js is called but the function ImportController is not called. exit loop the ImportController gets called with [3] in the input. the ImportController gets called with [3] in the input. the ImportController gets called with [3] in the input. 
2
  • 2
    You forgot to wait for button click to complete successfully before manipulating the input/button once more.
    – BalusC
    CommentedJun 3, 2024 at 19:06
  • I tried using async: false in the Ajax call. I thought this was supposed to make the JavaScript interpreter wait for the response, but it just didn't do anything after the first call. What would be your suggestion to wait for the response? I could do a setTimeout and wait for the data.status to be success in a loop, but wouldn't that be the same as async:false?
    – Rike M
    CommentedJun 3, 2024 at 19:18

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.