2017-03-03 1 views
2

빠른 도움이 필요합니다.양식 제출 후 결과 얻기

사용자가 csv 파일을 업로드 할 수있게하고 싶습니다. 그런 다음 파일의 구문 분석 및 의미있는 작업을 수행합니다. 마지막으로 결과를 다시 사용자에게 표시하십시오. 파일을 업로드 할 때 파일 크기가 < = 250kb인지 또는 < = 1000 행인지 확인하고 싶습니다. JSP에서

:

<form action="/Project/CSVUpload" method="post" enctype="multipart/form-data"> 
<br /> 
<input id="uploadfilebutton" type="file" name="file" class="tss-btn tss-btn-blue" accept=".csv" required /> 
<br /> 
<input id="processfilebutton" type="submit" value="Process File" class="tss-btn tss-btn-blue" /> 
</form> 

그래서 업로드 버튼과 제출 버튼이 있습니다. 사용자가 제출 버튼을 클릭 한 후에 상태를 다시 얻으려면 어떻게해야합니까? 예를 들어, 프로세스가 실패하면 팝업 오류 메시지를 표시하려고합니다. 자바 스크립트에서

: 나는이 양식을 사용하지 않는 경우

function process() 
{ 
$.ajax({ 
    url:"/Project/CSVUpload", 
    type:'POST', 
    contentType: 'application/json', 
    dataType: 'json', 
    success:function(soapContents){ 

     if (soapContents.haserrors) 
     { 
      BootstrapDialog.show({ 
       title: 'Process File Failed', 
       message: soapContents.returnmessage, 
       buttons: [ { 
        label: 'Ok', 
        action: function(dialogItself){ 

         dialogItself.close(); 
        } 
       }] 
      }); 
     } 

    } 
}); 
} 

이 작동합니다. enctype이 이와 같아야하기 때문에 양식이 필요합니다. 자바에서

:

@WebServlet("/CSVUploads") 
public class CSVUpload extends HttpServlet 
{ 

public void doPost(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, java.io.IOException 
{ 
boolean hasErrors = false; 
if (CSVUpload success) // assume we get the info from CSVUpload class 
{ 
    hasErrors = true; 
} 
if (hasErrors) 
    { 
     log.error("CSVUpload: "+returnMessage); 

     // Setup JSON response. 
     JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder(); 
     jsonObjectBuilder.add("haserrors", hasErrors); 
     jsonObjectBuilder.add("returnmessage", returnMessage); 
     JsonObject jsonObject = jsonObjectBuilder.build(); 

     // Write the JSON out as a response. 
     PrintWriter printWriter = response.getWriter(); 
     printWriter.print(jsonObject); 
     printWriter.flush(); 
    } 
} 

그래서 나는 약간의 변화를 만들어 아약스 쿼리를 처리하기 위해 새로운 자바 클래스를 추가 ... 그것은 먼저 양식 정보를 얻고 제출하고 두 번째 확인되어 제출 두 번 제출 성공 ... 누군가가 더 좋은 아이디어를 가지고 있는지 나는 모른다.하지만 이것이 내가 변화되도록 만드는 것이다. 좋은 생각은 아니지만 지금은 효과가 있습니다. 누구든지 더 좋은 아이디어가 있다면 알려주십시오. 작동하는 샘플이 좋습니다.

+0

유형 변경 = "제출"= "버튼"json 리턴 –

답변

1

제출 대신 버튼을 사용하십시오. 이것은 제출 유형 입력이 양식을 제출하기 때문이며, 또한 ajax를 사용하여 요청을 보냅니다. 그것이 두 가지 요청의 이유입니다.

+0

알겠습니다. 시도해 보겠습니다. 귀하의 회신에 감사드립니다. –

+0

당신은 type = "button"이 아닌 type = "submit"을 의미했습니다 ... 이제 얻습니다 ... 감사합니다. –