title: Auto upload image without page refresh and jquery in jsp
body: <pre><code>function showMyImage(fileInput){ 
      alert('inside showMyImage');
      var files = fileInput.files;
      for (var i = 0; i &lt; files.length; i++) {           
          var file = files[i];
          var imageType = /image.*/;     
          if (!file.type.match(imageType)) {
              continue;
          }           
          var img = document.getElementById("output");
  
          img.file = file;    
          var reader = new FileReader();
          reader.onload = (function(aImg) { 
              return function(e) { 
                  aImg.src = e.target.result;
                  //alert(aImg.src);
              }; 
          })(img);
          reader.readAsDataURL(file);
      }
  
      var formData = new FormData();
      formData.append( 'file', $('#artistprofileuploadfileField')[0].files[0]);
     // formData.append('image', $('input[type=file]')[0].files[0]);
      alert(formData);
      $.ajax({
          url: "/uploadImage",
          data:formData,
          type: "POST",
          contentType: false,//'multipart/form-data',
          processData: false,
          cache: false,
          success: function() {
              alert("success");
              document.getElementById("imgDiv").innerHTML=xmlhttp.responseText;
  
          },
          error: function() {
              alert("unable to create the record");
          }
      });
  
       //this.myform.submit();
  }
  </code></pre>
  
  <p>I am facing issue while auto uploading an image on jsp. I want to upload it and save it to a folder using ajax since I don't want to refresh the whole page but I would not prefer ajax using jQuery. I have a single form with enc type="multipart/form-data", in which I have many check boxes,textboxes,drop downs and image tags. 
  If I am saving other details of the form to the database then image is not getting saved to the folder and vice versa. I tried many times with some changes but could not be able to do it.
  Any help will be appreciated a lot. Thanks in advance... </p>
  
  <pre><code>    String saveFile="";
          String contentType = request.getContentType();
          if((contentType != null)&amp;&amp;(contentType.indexOf("multipart/form-data") &gt;= 0)){
          DataInputStream in = new DataInputStream(request.getInputStream());
          int formDataLength = request.getContentLength();
          byte dataBytes[] = new byte[formDataLength];
          int byteRead = 0;
          int totalBytesRead = 0;
          while(totalBytesRead &lt; formDataLength){
          byteRead = in.read(dataBytes, totalBytesRead,formDataLength);
          totalBytesRead += byteRead;
          }
          String file = new String(dataBytes);
          saveFile = file.substring(file.indexOf("filename=\"") + 10);
          saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
          saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
          int lastIndex = contentType.lastIndexOf("=");
          String boundary = contentType.substring(lastIndex + 1,contentType.length());
          int pos;
          pos = file.indexOf("filename=\"");
          pos = file.indexOf("\n", pos) + 1;
          pos = file.indexOf("\n", pos) + 1;
          pos = file.indexOf("\n", pos) + 1;
          int boundaryLocation = file.indexOf(boundary, pos) - 4;
          int startPos = ((file.substring(0, pos)).getBytes()).length;
          int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
          File ff = new File("C:/Users/Snigdha/Documents/NetBeansProjects/new_signups_recent/new_signups_1/web/pic/"+saveFile);
          FileOutputStream fileOut = new FileOutputStream(ff);
          fileOut.write(dataBytes, startPos, (endPos - startPos));
          fileOut.flush();
          fileOut.close();
   response.getWriter().write("Uploading done");
          sess.setAttribute("image_name", ff.getName());
          response.sendRedirect("signup_forms.jsp");
          }
  </code></pre>