ajax로 업로드 시 file객체 처리

-- JQuery 2015. 5. 11. 17:06
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
파일업로드 시 서버호출을 AJAX로 할 때 파일업로드가 실패한다.
이유는 파일객체를 서버로 제대로 전달하지 못해서 인데, FormData를 쓰면 제대로 파일객체를 전달할 수 있다.

// 전송하려는 form의 ID가 frm일 때
var form = new FormData(document.getElementById('frm')); 
              
$.ajax({dataType : 'json',
    url : '/test.jsp',
    data : form,
    type : 'text',
    processData: false,
    contentType:false,
    type: 'post',
    success : function(data) {
     
    },
    error : function(data) {
     
    }
});

 

FormData.append
Appends a new value onto an existing key inside a FormData object, or adds the key if it does not already exist.
FormData.delete
Deletes a key/value pair from a FormData object.
FormData.get
Returns the first value associated with a given key from within a FormData object.
FormData.getAll
Returns an array of all the values associated with a given key from within a FormData.
FormData.has
Returns a boolean stating whether a FormData object contains a certain key/value pair.
FormData.set
Sets a new value for an existing key inside a FormData object, or adds the key/value if it does not already exist.

'-- JQuery' 카테고리의 다른 글

$.each 메소드 사용시 break, continue 사용  (0) 2017.01.24
JQuery data()  (0) 2015.11.16
when, done, then  (0) 2014.11.24
IE8, IE9에서 placeholder 적용되게 하는 방법  (0) 2014.09.02
특정 레이어만 프린트  (0) 2014.08.22
posted by 어린왕자악꿍