So if there are more file it will display only one file and if there are less than 5 files it will show an error. Could you please help me to deal with this issue?
There are a few paths to dealing with the issue you are trying to resolve. The most straightforward would be to have a query for each case you need since it is limited to 5 it isn't too hefty of a task.
Another way would be to build your form data as part of a transformer/jsquery and then send the result regardless of the number of files attached.
I'd say since you know that there will be at least 1 and up to 5 files to process gives you a good framework for developing logic to call upon the right query. A chained ternary operation or a switch/case could work for that: