0

I have a form in which I use four <input type="file" multiple> and I want to save these files in a folder and the path in database.

But, the problem is how I came to know the number of files selected by user in each file browser?

My code is:

<form action="<%=request.getContextPath()%>/AddPhotoServlet" method="post" enctype="multipart/form-data">
                <table style="font-size: 13px;">
                    <input type="hidden" name="j_ID" value ="<%request.getParameter("jid");%>">
                    <tr>

                        <td>Photo</td> <td><input type="file" id="photo" value="photo" name="files[]" size="20" multiple="multiple" required=""></td>
                    </tr>
                    <tr>
                        <td>Deed</td> <td><input type="file" id="deed" value="deed" name="files[]" size="20" multiple="multiple"></td>

                    </tr>
                    <tr>

                        <td>Plan</td> <td><input type="file" id="plan" name="files[]"value="plan" size="20" multiple="multiple"></td>

                    </tr>
                    <tr>

                        <td>Working</td> <td><input type="file" id="working" name="files[]" value="working" size="20" multiple="multiple"></td>

                    </tr>>
                    <td colspan="6" align="center"><center><input type="submit" value="Upload" onclick="counter()">
                        </tr>
                        </table>
                        </form>

And, I also use Javascript:

<script>

            function myCounter()
            {


                var photo = document.getElementById('photo');


                alert("number of files" + photo.files.length)
                for (var i = 0; i < photo.files.length; ++i) {
                    var name = photo.files.item(i).name;
                    alert("here is a file name: " + name);

                }

            }

I get number of files by using Javascript. I want to use photo.files.length in my Servlet.

Unheilig
  • 16,196
  • 193
  • 68
  • 98
vinay kaushik
  • 129
  • 1
  • 8
  • 1
    Send it as a query parameter. – Suresh Atta Oct 29 '15 at 06:47
  • 1
    You can set its value to some `hidden` field in your form in your JavaScript function. Which you can retrieve in your servlet from request object. – Naman Gala Oct 29 '15 at 06:52
  • but how will i got the objects of HttpRequest and HttpResponse on my servlet. i also want that multipart requsest should be available in my servlet class . – vinay kaushik Oct 29 '15 at 06:57
  • [for getting javascript value to servlet](http://stackoverflow.com/questions/2132242/need-to-call-servlet-from-javascript-along-with-parameters?rq=1) and many others, [handling file uploads](http://stackoverflow.com/questions/2422468/how-to-upload-files-to-server-using-jsp-servlet). Marking this as a duplicate, this is FAQ material. You have two questions there, getting a javascript value to servlet and sending files, which both have been answered in multiple other questions. – eis Oct 29 '15 at 07:26
  • **I want to use photo.files.length in my servlet.** so basically you want to access a javascript variable as generated by the browser in the code running on the server? – Gimby Oct 29 '15 at 08:44
  • yes ! is it possible – vinay kaushik Oct 29 '15 at 09:35

0 Answers0