2016-11-16 15 views
0

我想使用上傳的文件類型輸入文件發送,我也有另一種PARAM我想我下面的表單代碼提交試圖上傳文件,但沒有被要求

<form id="fileupload" action="uploadMassContentsFromExcelSheetNew" method="post" 
    enctype="multipart/form-data" onsubmit="return doMassValidate();" >   

     <input type="hidden" id="fileName" name="fileName" value=""/> 
     <input type="hidden" id="serviceIdMass" name="serviceIdMass" value=""/> 
     <input type="hidden" id="contentTypeMass" name="contentTypeMass" value=""/> 



    <div id='fileDiv' class="row fileupload-buttonbar"> 
     <div class="col-md-12 col-xs-12 col-lg-12"> 
     <div class="input-group input-group-file"> 
     <input type="text" class="form-control" 
         disabled="disabled" id="uploadFile" name="uploadFile"> 
     <span class="input-group-btn"> 
      <span class="btn btn-primary btn-file"> 
     <i class="icon md-upload" aria-hidden="true"></i> 
     <input type="file" name="files" id="uploadBtn"> 
      </span> 
      </span> 
     <p style="word-wrap: break-word;"> 
      <small id="fileError" style="display: none;" 
          class="help-block" data-fv-validator="notEmpty" 
          data-fv-result="VALIDATED"> 
       <s:property value="getText('content.file.error')" /> 
      </small> 
     </p> 

     </div> 
     </div> 
    </div> 

      <button name="sub" type="submit" >submit</button> 

</form> 

JS代碼:

function doMassValidate(){ 
    var hasSelectedFile=checkIfHasSelectedFile(); 
    if(!hasSelectedFile){ 
     fileErrorMessage(); 
     $("#fileError").hide(); 

     return false; 
    } 
    else{ 
     removeErrorClass('fileDiv'); 
     return true; 
    } 
} 
function checkIfHasSelectedFile(){ 
    var hasNotUploadFile= $('#uploadBtn').val(); 
    if(hasNotUploadFile=="" || hasNotUploadFile=='') 
     return false; 
    else return true; 

} 

這是我的行動:

public String uploadMassContentsFromExcelSheetNew() { 
    HSSFSheet sheet = null; 
    HSSFWorkbook workbook = null; 
    FileInputStream inputStream = null; 
    result=new JSONObject(); 

    try { 
      if (fileName != null){ 
      fileName=fileName.trim(); 

     if (fileName.endsWith(".xls") || fileName.endsWith(".xlsx")) { 

    HttpServletRequest request = ServletActionContext.getRequest(); 
       System.out.println("Parts*******:"+request.getParts()); //return null 


       MultiPartRequestWrapper multiWrapper=(MultiPartRequestWrapper)ServletActionContext.getRequest(); 
      String fileNameParam=servletRequest.getParameter("fileName");//i get the file name value 
String fileParam=servletRequest.getParameter("files");// i cant get param with name "files" when i inspect the servletRequest 

      Part filePart =request.getPart("files");//return null 
      File[] f=multiWrapper.getFiles("files"); //return null 
      String fileName=multiWrapper.getFileNames("files")[0]; 
      Enumeration fileParameterNames = multiWrapper.getParameterNames(); 
      String param = request.getParameter("files"); 
     } 
      } 
    } 
    catch(Exception ex){ 
     ex.printStackTrace(); 
    } 
}  

和IM使用的FileUpload攔截:

<interceptor-ref name="fileUpload"/> 
<interceptor-ref name="basicStack"/> 

這是請求:

------WebKitFormBoundaryjBpjIWpSdq4RzL0X 
Content-Disposition: form-data; name="fileName" 

C:\fakepath\SMSMassploadOladDate.xls 
------WebKitFormBoundaryjBpjIWpSdq4RzL0X 
Content-Disposition: form-data; name="serviceIdMass" 

647 
------WebKitFormBoundaryjBpjIWpSdq4RzL0X 
Content-Disposition: form-data; name="contentTypeMass" 

1 

------WebKitFormBoundaryjBpjIWpSdq4RzL0X 
Content-Disposition: form-data; name="files"; filename="" 
Content-Type: application/octet-stream 

不宜文件內容類型是 「application/ms-excel,application/vnd.ms-excel」 代替application/octet-stream?和文件名不應該是空的?

我真的不能得到什麼是錯的文件?

+0

究竟是什麼問題?請求無法啓動?我想這個問題是在你的JavaScript中,也許是因爲你正在閱讀假字段而不是讀取輸入文件的值。嘗試與'var hasNotUploadFile = $('#uploadBtn')。val();' –

+0

問題是我無法獲得文件沒有被髮送到服務器端,當請求文件的值爲空。 我已更新腳本以檢查用戶是否上傳了文件。 – Rasha

+0

首先,嘗試徹底刪除腳本,並正常發送文件。它工作嗎? –

回答

0

我已經解決了這個問題,在struts.xml中我只是擴展了「struts-default」,它應該擴展爲「struts-default,json-default」。 也與其相關的blueimp插件,我試圖使用,「main.js」庫被調用,並使我的代碼無法正常工作。

相關問題