0

I have such html code

<input name="childLogin" type="text" placeholder="Login" id="login"/>
    <p class="validation" id="loginVal"></p>

    <input name="childPassword" type="password" placeholder="Password" id="password"/>
    <p class="validation" id="passVal"></p>


    <input name="childName" type="text" placeholder="Full name" id="fullName"/>
    <p class="validation" id="nameVal"></p>


    <p> Gender <select id="gender" name="gender">
        <option>
            Male
        </option>
        <option>
            Female
        </option>
    </select></p>


    <p>Select photo <input type="file" id="photo" name="photo" style="width:200px;border: none"/></p>
    <button  id="button">Add</button>

And some javascript code

document.getElementById("button").onclick = function () {


        var image = document.getElementById('photo').value;
        var gender = document.getElementById('gender').value;
       var log = document.getElementById('login').value;
      var password = document.getElementById('password').value;
       var fullName = document.getElementById('fullName').value;
  $.ajax({
                method: "post",
                url: "http://localhost:8080/servlets.AddChildServlet",
                data: {login: log, password: password, fullName: fullName, gender: gender, image: image}
            })
                    .done(function (msg) {
                        alert(image);

                    });

How can I pass the image file to the servlet? And what should I write in my servlet to retrieve the image as inputStream?

A Sdi
  • 665
  • 2
  • 10
  • 24
Anastasia
  • 3
  • 1
  • 1
    your main question "via ajax without using html forms" don't match your code. With Javascript you access the html form fields and send the data to the server with ajax. – code_angel Nov 30 '16 at 18:41
  • Maybe I don't express myself clearly. Do you have any ideas how can I pass the image file to servlet and retrieve it to add to db? – Anastasia Nov 30 '16 at 19:29

0 Answers0