如何通过struts2中的ajax上传文件

时间:2016-12-28 07:17:53

标签: javascript jsp 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

}

1 个答案:

答案 0 :(得分:1)

您可以使用formdata上传代码或任何jquery文件上传插件 https://stackoverflow.com/a/204271/876739

您可以看到此帖子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" />