我希望通过ajax上传img,我的php无法收到我的ajax帖子。
function handleFileSelect(evt) {
files = evt.target.files; // FileList object
$('.thumb-canvas' + filesId).css('display','block');
// Loop through the FileList and render image files as thumbnails.
for (var i = 0, f; f = files[i]; i++) {
// Only process image files.
if (!f.type.match('image.*')) {
continue;
}
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = (function(theFile) {
return function(e) {
// Render thumbnail.
var span = document.createElement('span');
span.innerHTML = ['<img class="thumb" src="', e.target.result,
'" title="', escape(theFile.name), '"/>'].join('');
document.getElementById('list' + filesId).insertBefore(span, null);
};
})(f);
// Read in the image file as a data URL.
reader.readAsDataURL(f);
//upload ajax
var xhr = new XMLHttpRequest();
var formData = new FormData();
formData.append('file',files);
xhr.upload.addEventListener("loadstart", function(e){
$("#progressbar").show();
$("#percentage").show();
}, false);
xhr.open("POST", "upload.php");
xhr.overrideMimeType('text/plain; charset=x-user-defined-binary');
xhr.send(formData);
}
}
我的php
echo var_dump($_FILES['file']);
我得到了NULL。我也试过echo var_dump($_FILES['files']);
我尝试使用$ _POST来检查是否有东西,是的,它不是空的,但我想知道为什么我不能使用$ _FILES
答案 0 :(得分:-1)
'text/plain; charset=x-user-defined-binary'
这不是POST文件传输的数据编码,因此PHP无法找到文件,因为数据必须是多部分表单数据。
您可以让脚本正确发送,然后PHP会将文件发现到$ _FILES。或者您可以进行自定义数据传输(类似于此处Using HTML5 file uploads with AJAX and jQuery)