2
我試圖在JavaScript中使用drag and drop plugin來使用ajax上傳文件。使用XMLHttpRequest上傳文件
<script>
DnD.on('#drop-area', {
'drop': function (files, el) {
el.firstChild.nodeValue = 'Drag some files here.';
var names = [];
[].forEach.call(files, function (file, i) {
names.push(file.name + ' (' + file.size + ' bytes)');
var xhr = new XMLHttpRequest();
xhr.open('POST','upload.php');
xhr.setRequestHeader("Content-type", "multipart/form-data");
xhr.send(file);
console.log(xhr.responseText);
});
document.querySelector('#dropped-files p i').firstChild.nodeValue = names.join(', ');
}
});
</script>
而這裏的upload.php的:
<?php
print_r($_POST);
?>
基本上我沒有寫腳本上傳的文件還沒有,因爲我還是搞清楚我如何可以訪問數據,我已通過JavaScript發送。你能指導我下一步該做什麼?如何從upload.php訪問文件。
我還沒有看到任何東西當我console.log(xhr.responseText); – user225269
什麼是您的網絡瀏覽器?請注意,XMLHttpRequest在IE中不起作用。 –
我正在使用鉻金絲雀。感謝您的所有幫助,但它仍然無法正常工作,好像沒有文件正在發送。 – user225269