3

I am trying to make an upload page which takes file to upload. I am using Spring framework here my query is on Upload button I am calling a JavaScript method which should send my file to controller using jQuery AJAX. Is there any way to pass this through JavaScript?

Following is the code which I am trying.

<body>
    <div style="text-align: center; margin-top: 60px;">
        <form enctype="multipart/form-data">
            Select file:
            <input type="file" name="dataFile" id="fileAttachment"/><br/><br/>
                <div style="text-align: center; margin-top: 100px;">
                    <input style="cursor: pointer;" onmouseover="" onclick="uploadAttachment()" class="dialogbox" type="submit" value="Upload Report" />
                </div>
        </form>
    </div>
</body>

JS:

<script language="Javascript">
function uploadAttachment(){
    var Name = jQuery('#Name option:selected').text();
    jQuery.post('upload',{Name:Name}
    function(data){
    if(data==1)
    alert("success");
    else
    alert("failed");
    });
}
    </script>

on controller.java page following is the code written

@RequestMapping(value = "upload", method=RequestMethod.POST)
        public @ResponseBody String upload(HttpServletRequest request, HttpServletResponse response,
@RequestParam("Name") String Name){
System.out.println(Name);
}
4
  • What exactly you want.Do you want to show the uploaded file detail in the same page ? Just like gmail shows the attachment details ? Commented Feb 24, 2014 at 11:56
  • 1
    i just want to send the file selected for upload to controller via Jquery.post() Commented Feb 24, 2014 at 12:03
  • 1
    Can you post your controller code ? Commented Feb 24, 2014 at 12:31
  • @HumanBeing : above i pasted the example of my controller where i am sending String object to controller, now is there any way to send file object to controller ? Commented Feb 25, 2014 at 11:35

1 Answer 1

0

I think you can try the following code to upload file with javascript.

function uploadAttachment(){
  $('#fileAttachment').trigger('click');

  if (typeof timer != 'undefined') {
      clearInterval(timer);
  }

  timer = setInterval(function() {
      if ($('#fileAttachment').val() != '') {
          clearInterval(timer);

          $.ajax({
              url: 'YOUR_UPLOAD_URL',
              type: 'post',
              dataType: 'json',
              data: new FormData($('#fileAttachment').closest('form')[0]),
              cache: false,
              contentType: false,
              processData: false,
              success: function(response){
              }
          });
      }
  }, 500);
}

You need to replace YOUR_UPLOAD_URL with your server upload URL.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.