Jani
Jani

Reputation: 1

How to upload a file through ajax in struts2

<s:file name="excel" id="excel"/> 
function saveData(){ 
    vald();             
    var postData = $("#fname").serializeArray();            
    $("#fname")[0].reset();             
    $('<div class="success-tog" >Processing...</div>').prependTo('body').delay(1000); 
    $.ajax({type: "POST",url: "",data: postData success: sucFunc()}); 

Script Code

}

Upvotes: 0

Views: 2317

Answers (1)

xrcwrn
xrcwrn

Reputation: 5327

You can use formdata to uplaod code or any jquery file upload plugin https://stackoverflow.com/a/204271/876739

You can see this post https://stackoverflow.com/a/41458116/876739

$(document).on('click', '#upload', function(e) {
          e.preventDefault();
      var fd = new FormData();
      var file = $('#my_file')[0].files[0];
      fd.append('file', file);
      fd.append('userId', $('#userid').val());
      console.log("hi");
      $.ajax({
          url: 'UploadPic',
          data: fd,
          type: "POST",
          contentType: false,
          processData: false,
          success: function(dd) {
            alert("sucessfully Uploaded")
            }
          });
      });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="file" id="my_file">
<input type="hidden" id="userid" />
<input type="button" id="upload" value="Upload" />

Upvotes: 1

Related Questions