我正在尝试使用拖放插件 http://pascalprecht.github.com/DnD.js/在javascript中使用ajax上传文件。
<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 访问该文件。
尝试使用FormData
代替xhr
:
var formData = new FormData();
formData.append("thefile", file);
xhr.send(formData);
您可以通过此访问您的文件array
:
<?php var_dump($_FILES["thefile"]); ?>
查看更多:http://www.w3schools.com/php/php_file_upload.asp http://www.w3schools.com/php/php_file_upload.asp
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)