我正在尝试使用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访问文件。
XMLHttpRequest
不起作用。 - Afshin MehrabaniFormData
?回答已编辑,请再次检查。 - Afshin Mehrabanierror_handling
并将其设置为E_ALL
,现在您可能会看到一些错误或警告。 如果这没有帮助,请参见此问题:http://stackoverflow.com/questions/5659340/ajax-uploading-a-file-html-5-php 在您的标头中(xhr请求中),添加X-File-Type
和X-File-Name
,并在xhr.open
的第三个参数中填写true
。 - Afshin Mehrabani