我一直在通过 jQuery AJAX 编码文件上传。一切正常,我试图上传大约 4 MB 的文件。我错过了什么?
事实如下:
- php.ini - 检查(最大上传文件大小,最大执行时间等)
- 处理文件上传的服务器端脚本已执行但
$_POST
为$_FILES
空 - var
fd
似乎处理正确
jQuery块:
var x_upload = $(".x_upload");
x_upload.on('drop', function(e) {
e.preventDefault();
var files = e.originalEvent.dataTransfer.files;
handleFileUpload(files, x_upload);
});
function handleFileUpload(files, x_area) {
for (var i = 0; i < files.length; i++)
{
var fd = new FormData();
fd.append('file', files[i]);
console.log(files[i]); //is OK
fd.appdend('type', 'image');
sendFileToServer(fd, x_area);
}
}
function sendFileToServer(formData, x_area) {
var uploadURL = "/api/upload.php";
var jqXHR = $.ajax({
xhr: function() {
var xhrobj = $.ajaxSettings.xhr();
if (xhrobj.upload) {
xhrobj.upload.addEventListener('progress', function(event) {
var percent = 0;
var position = event.loaded || event.position;
var total = event.total;
if (event.lengthComputable) {
percent = Math.ceil(position / total * 100);
}
//Set progress
x_area.find(".x_progress_num").text(percent);
}, false);
}
return xhrobj;
},
url: uploadURL,
type: "POST",
contentType: false,
processData: false,
cache: false,
data: formData,
success: function(data) {
console.log(data);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
console.log("AJAX Error " + errorThrown + " " + textStatus);
x_area.addClass('x_error');
}
});
}
简化的服务器端
<?php
var_dump($_POST); // is empty if file is >4 MB
var_dump($_FILES); // is empty if file is >4 MB
if (!isset($_POST['type'])) {
echo "empty type";
} else {
//echo "ok' only if file is ls <4MB
echo "ok";
}
那么可能有我忽略的任何文件大小限制吗?谢谢你。