2011-11-26 2 views
1

jQuery를 사용하여 파일을 업로드하려고합니다. 나는 Input을 포함하는 숨겨진 양식을 사용하고 jQuery를 사용하여 파일 브라우저를 시작합니다. 입력이 변경되면 파일은 PHP 서버 스크립트에 게시됩니다.Jquery file posting help

내 문제는 내가 방금하면 $("form1").submit(); 모든 것이 완벽하지만 내 페이지가 리디렉션됩니다. 리디렉션을 피하기 위해 PHP 스크립트에서 응답 텍스트를 얻고 싶습니다. 따라서 jQuery Post 함수를 사용했지만 이번에 PHP 스크립트는 파일 게시가 있다는 것을 인식하지 않고 theres가 ($_FILES) = nothing이 될 때마다 오류를 반환합니다.

내 HTML 코드 :

<form id="form1" name="up1" method="POST" action="upload.php" enctype="multipart/form-data"> 
<input style="width:0px;height:0px"id="br" type="file" name="Filedata"/> 
</form> 

jQuery를 :

$("#br").change(function() { 
    var $el = $('#br'); 
    $('#form1').submit(); 
}); 
//The code above works perfectly if I dont add this code below. 
$("#form1").submit(function(event) { 
event.preventDefault(); 
var $el = $('#br'); 
var $form = $(this), 
term = $form.find('input[name="Filedata"]').val(), 
url = $form.attr('action'), 
enctype= "multipart/form-data"; 

$.post(url, { s: term }, 
    function(data) { 
    alert(data); 
    } 
); 
}); 
+0

이것 좀보세요 http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery – Cyclonecode

답변

1

그것의 Ajax를 사용하여 파일을 업로드 할 수 없다. 그러나 숨겨진 iframe을 만들고 iframe에 Form의 대상을 설정하는 트릭이 있습니다. 파일 업로드가 완료된 시점을 알려면 iframe의 readyState 속성을 사용하십시오. 진짜 Ajax는 아니지만 똑같은 모양입니다.