AJAX를 통해 d' n'd하여 서버에 폴더를 업로드하고 싶습니다. 하지만 이미 업로드 파일에 문제가 있습니다.어떻게 Ajax를 통해 파일을 전송할 수 있습니까?
나는 파일이나 폴더를 확인하기 위해 e.dataTransfer.items
과 webkitGetAsEntry()
을 사용합니까?
파일 인 경우 기능이 traverseFileTree
인데 파일이 있지만 을 formData
에 추가 할 수 없습니다.
e.dataTransfer.files
을 사용하는 경우 무엇인지 알 수 없습니다. webkitGetAsEntry()
오류가 발생하여 파일 또는 폴더.
내가 뭘 잘못 했니? 전역 배열 $ _FILES로 파일을 전송하는 방법.
소스 (upload.php로) :
echo "<pre>";
print_r ($_FILES);
echo "</pre>";
소스 (index.html을) :
<!DOCTYPE html>
<html>
<head>
<title>Drag and Drop</title>
<style>
body {
background: rgba(211,211,100, .5);
font: 20px Arial;
}
.dropzone {
width: 300px;
height: 300px;
border: 2px dashed #aaa;
color: #aaa;
line-height: 280px;
text-align: center;
position: absolute;
left: 50%;
margin-left: -150px;
top: 50%;
margin-top: -150px;
}
.dropzone.dragover {
color: green;
border: 2px dashed #000;
}
</style>
</head>
<body>
<p>Loaded files:</p>
<div id="uploads">
<ul>
</ul>
</div>
<div class="dropzone" id="dropzone">Drop files</div>
<script>
(function() {
var formData = new FormData();
var dropzone = document.getElementById("dropzone");
dropzone.ondrop = function(e) {
this.className = 'dropzone';
this.innerHTML = 'Drop files';
e.preventDefault();
upload(e.dataTransfer.items);
};
function traverseFileTree(item, path) {
path = path || "";
if (item.isFile) {
item.file(function(file) {
console.log(file); // show info
formData.append('file[]', file); // file exist, but don't append
});
} /*else if (item.isDirectory) {
var dirReader = item.createReader();
dirReader.readEntries(function(entries) {
for (var i=0; i<entries.length; i++) {
traverseFileTree(entries[i], path + item.name + "/");
}
});
}*/
}
var upload = function(items) {
var xhr = new XMLHttpRequest();
for(var i = 0; i < items.length; i++) {
var item = items[i].webkitGetAsEntry();
if (item) {
traverseFileTree(item,'');
}
}
xhr.onload = function() {
console.log(this.responseText);
};
xhr.open('post', 'upload.php');
xhr.send(formData);
};
dropzone.ondragover = function() {
this.className = 'dropzone dragover';
this.innerHTML = 'Mouse up';
return false;
};
dropzone.ondragleave = function() {
this.className = 'dropzone';
this.innerHTML = 'Drop files';
return false;
};
})();
</script>
그래서, 당신은이 크롬 및 기타 웹킷 기반 브라우저에서 작동하고 싶어? –
@JaromandaX firefox는 폴더 업로드를 지원합니다. [파일 선택 및/또는 파싱 할 폴더 선택] (http://stackoverflow.com/questions/36842425/select-drop-files-and-or-folders-to-be) -parsed /) – guest271314
@JaromandaX 네, 크롬에서 작동하길 원합니다 .... 나는 상관하지 않습니다. 나는 단지 그것이 일하기를 바란다. 나는 언젠가 그것을 해결할 수 없다. 나는 dropBox와 같은 일을하고 싶습니다. 사용자가 폴더를 드롭 할 수 있습니다. –