진행율이 퍼센트로 표시된 비동기 파일 업로드 양식을 만들려고합니다. 이 FormData 개체를 사용할 수도 생각했지만 게시물을 작동하지 않는다고 생각합니다. 제가 제출할 때 아무 일도 일어나지 않습니다. 내가 확인한 버그가 없으며 자바 스크립트없이 작동하므로 PHP는 정상적으로 코드에 근본적으로 잘못된 것이 있습니까?XMLHttprequest를 사용하여 여러 파일 업로드
var handleUpload = function(event){
event.preventDefault();
event.stopPropagation();
var fileInput = document.getElementById('file');
var data = new FormData();
for(var i = 0; i < fileInput.files.length; ++i){
data.append('file[]',fileInput.files[i]);
}
var request = new XMLHttpRequest();
request.upload.addEventListener('progress',function(event){
if(event.lengthComputable){
var percent = event.loaded/event.total;
var progress = document.getElementById('upload_progress');
while (progress.hasChildNones()){
progress.removeChild(progress.firstChild);
}
progress.appendChild(document.createTextNode(Math.round(percent * 100) + '%'));
}
});
request.upload.addEventListener('load',function(event){
document.getElementById('upload_progress').style.display = 'none';
});
request.upload.addEventListener('error',function(event){
alert("failed");
});
request.open('POST','upload.php');
request.setRequestHeader('Cache-Control','no-cache');
document.getElementById('upload_progress').style.display = 'block';
};
window.addEventListener('load',function(event){
var submit = document.getElementById('submit');
submit.addEventListener('click',handleUpload);
});
html로 :
<div id = "upload_progress"></div>
<form id="form" action="" method="post" enctype="multipart/form-data">
<input id="file" name="file[]" type="file" multiple /><br>
<input type="submit" name="send" id ="submit" value="send">
</form>
하고 upload.php로 당신의 자바 스크립트 코드에서 가장 중요한 라인을 잊어 버린
if(!empty($_FILES['file'])){
foreach ($_FILES['file']['name'] as $key => $name) {
move_uploaded_file($_FILES['file']['tmp_name'][$key],"myfiles/$name");
}
}
내가 깜빡 잊을 수 없다. 모자. 물론 충분히 작동합니다. 그걸 보아 주셔서 고맙습니다. – user2014429