2012-03-02 58 views
4

我上傳文件使用與jsp作爲前端的struts 2,但我不想在文件上傳後刷新頁面,所以我使用的是Ajax但我無法獲取File對象的動作,似乎文件上傳需要在jsp中形成標籤,並且如果我提交表單,那麼頁面會被刷新。 我通過網絡進行了研究,但無法獲得許多相關結果,如果有人引導我完成這個任務,這對我有很大的幫助。任何幫助真的會被讚賞。使用的struts2和iframe 問候文件上傳與Ajax的struts 2

+0

是否嘗試該例子中[FileUploadUsingAjax](http://code.google.com/p/ajax-file-upload-struts2/downloads/list) – 2012-03-02 14:13:12

+0

這些是罐子和壓縮包,需要做什麼,請你指導一下什麼是適應流程? – 2012-03-02 14:23:39

+1

@EricIlavia:遵循這個http://stackoverflow.com/questions/5270288/upload-file-in-struts2-using-ajax – 2012-03-02 17:15:43

回答

0

我建議使用上傳文件,而不是AJAX iframe中上傳的CSV文件

示例代碼:

var file = $("#fileUpload").val(); 
    if(file.indexOf(".") != -1 && file.substr(file.indexOf("."))==".csv"){ 
     /* created IFrame For UPload file*/ 
     var iframe = $('<iframe name="uploadIPAddressIFrame" id="uploadIPAddressIFrame" style="display: none" />'); 
     $("body").append(iframe); 

     /* Set Form for submit iframe*/ 
     var form = $('#ipPoolForm'); 
     form.attr("action", "uploadCSVFile.do"); 
     form.attr("target", "uploadIPAddressIFrame"); 
     form.submit(); 

     openDialog(title); 
     /* handle response of iframe */ 
     $("#uploadIPAddressIFrame").load(function() { 
      response = $("#uploadIPAddressIFrame")[0].contentWindow.document.body.innerHTML; 
      $("#chkIPAddressDiv").html(response); 
      $("iframe#uploadIPAddressIFrame").remove(); 
     });  

上傳後,如果您提交表單,然後更改目標形式:

// Because of using iframe for upload set target value 
$("#ipPoolForm").attr("target", "");