2012-11-30 5 views
0

Spring MVC에서 메소드를 매핑하는 URL에 Ajax 요청을 보내는 다음 js 코드가 있습니다.Spring MVC에서 Ajax를 통해 PUT 메소드를 사용하여 파일을 업로드하는 방법은 무엇입니까?

function update(id) 
{ 
    $.ajax({ 
     datatype:"json", 
     type: "put", 
     url: "/wagafashion/ajax/TempAjax.htm", 
     data: "id=" + id+"&t="+new Date().getTime(), 
     success: function(response) 
     { 
      alert(response);       
     }, 
     error: function(e) 
     { 
      alert('Error: ' + e); 
     } 
    }); 
} 

다음은 파일 브라우저와 버튼 만있는 간단한 스프링 양식입니다.

<form:form id="mainForm" name="mainForm" method="post" action="Temp.htm" enctype="multipart/form-data" commandName="tempBean"> 
    <input type="file" id="myFile" name="myFile"/> 
    <input type="button" id="btnSubmit" name="btnSubmit" onclick="update(1);" value="Submit"/> 
    <!--The js function is called when this button is clicked supplying 1 as id.--> 
</form:form> 

버튼을 누르면 다음과 같은 메소드가 호출됩니다.

@RequestMapping(method={RequestMethod.PUT}, value={"ajax/TempAjax"}) 
public @ResponseBody String update(HttpServletRequest request, HttpServletResponse response) 
{ 
    System.out.println(ServletFileUpload.isMultipartContent(request)); 
    return "Message"; 
} 

ServletFileUpload.isMultipartContent(request) 그러나 false 반환 메서드 호출.


다음과 같이 내가 방법을 수정 ,

@RequestMapping(method={RequestMethod.PUT}, value={"ajax/TempAjax"}, headers={"content-type=multipart/form-data"}) 
public @ResponseBody String update(@RequestParam MultipartFile file, HttpServletRequest request, HttpServletResponse response) 
{ 
    System.out.println(ServletFileUpload.isMultipartContent(request)); 
    return "Message"; 
} 

는 JS 코드에서 오류 부분은 항상 Error: [object Object]을 알려줍니다. 이 경우 POST 메서드가 사용 된 경우에도 동일한 문제가 발생합니다.

Ajax를 통해 멀티 파트 콘텐츠를 전달하는 방법 (정확히 PUT 메서드 사용)?

답변

0

이것이 멀티 파트 파일을 서버에 게시하는 방법을 실제로 볼 수 없습니까? 데이터에는 ID와 시간 만 포함됩니다.

같은 것을보십시오 :

function update(id) 
{ 
    $.ajax({ 
     datatype:"json", 
     type: "put", 
     url: "/wagafashion/ajax/TempAjax.htm", 
     data: $('#mainForm').serialize(), 
     success: function(response) 
     { 
      alert(response);       
     }, 
     error: function(e) 
     { 
      alert('Error: ' + e); 
     } 
    }); 
} 
+0

만을 말한다 오류 섹션의 경고 상자, "* 오류 : [개체 개체] *"심지어 데이터'로 :. $ ('#의 mainForm') 직렬화() ,', 운이 없다. – Tiny

+0

스프링 컨텍스트에서 멀티 파트 해결 프로그램을 정의 했습니까? 여기에 설명 된대로 http://stackoverflow.com/a/13405415/302387 –

+0

예 'applicationContext.xml' 파일에'MultipartResolver'를 정의했는데 Ajax없이 제대로 작동하지만 JavaScript는 항상 "Error : [object Object] * ", Ajax의 경우. – Tiny