我认为你的问题是这样的:
data: {test_form},
测试您是否在服务器端获取任何文件打印文件变量的输出:
echo var_dump($_FILES);
并记下您的结果。
但:
我有一个不同的脚本来处理所有表单元素:
function ToAjax(form) { //target form
var jsonObj = [];
var formData = new FormData();
//Form element
$('[name="' + form + '"] *').not(':input[type=file], :input[type=button], :input[type=submit], :input[type=reset], :input[type=checkbox]:not(:checked)').filter(':input').each(function(obj, v) {
var iObj = {};
var input = $(this);
var inputname = input.attr("name");
if (typeof inputname !== typeof undefined && inputname !== false) { //handle attribute names
inputname = inputname.replace(/-/g, '_'); //handle underscore
var val = input.val();
var inputval = val;
iObj[inputname] = inputval;
jsonObj.push(iObj);
}
});
//Summernote textarea editor
$('[name="' + form + '"] *').filter('div.sumertextarea').each(function(obj, v) {
var iObj = {};
var input = $(this);
var inputname = input.attr("data-name");
inputname = inputname.replace(/-/g, '_');
var val = input.summernote('code');
var inputval = val;
iObj[inputname] = inputval;
jsonObj.push(iObj);
});
//compres in json data
jsonData = JSON.stringify(jsonObj);
//parse json to formdata
formData.append('jsonData', jsonData);
//handle all file types input
$('[name="' + form + '"] *').filter(':input[type=file]').each(function(obj, v) {
if (typeof v.files[0] != 'undefined') {
var file = v.files[0];
var name = $(v).attr("name");
name = name.replace(/-/g, '_');
formData.append(name, file);
}
});
//handle all File type from dropzone plugin
$('[name="' + form + '"] *').filter('div.dropzone').each(function() {
var DropFile = $(this)[0].dropzone.getAcceptedFiles();
for (Nof in DropFile) {
var file = DropFile[Nof];
var name = DropFile[Nof].name;
name = name.replace(/-/g, '_');
formData.append(name, file);
}
});
//send all data packet to send ajax function.
window.SendAjax(formData);
}
发送到服务器端我使用这个:
function SendAjax(formdata) {
var url = 'index.php';
$.ajax({
url: url,
data: formdata,
contentType: false,
processData: false,
type: 'POST',
cache: false,
error: function(xhr, status, error) {
console.log(error);
alert(error);
},
success: function(resp) {
console.log(resp);
alert(resp);
}
});
}
是的,我用的是contenType: false
代替contentType: 'multipart/form-data',
因为我可以在服务器端更轻松地处理数据,访问你有 2 seccion 文件系统或 dropzone 插件的文件检查结果数组差异并照顾适应或在你的项目中使用它:
2文件示例同时发送,PDF和JPG $_FILES通用格式和dropzone插件的这个输出测试:
["i_file"]=>
array(5) {
["name"]=>
array(2) {
[0]=>
string(10) "test.pdf"
[1]=>
string(10) "test.jpg"
}
["type"]=>
array(2) {
[0]=>
string(15) "application/pdf"
[1]=>
string(10) "image/jpeg"
}
["tmp_name"]=>
array(2) {
[0]=>
string(24) "C :\ xampp\tmp\phpDDC5.tmp"
[1]=>
string(24) "C :\ xampp\tmp\phpDDC6.tmp"
}
["error"]=>
array(2) {
[0]=>
int(0)
[1]=>
int(0)
}
["size"]=>
array(2) {
[0]=>
int(180572)
[1]=>
int(8984)
}
}
使用 dropzone 插件:
["test_jpg"]=>
array(5) {
["name"]=>
string(10) "prueba.jpg"
["type"]=>
string(10) "image/jpeg"
["tmp_name"]=>
string(24) "C :\ xampp\tmp\phpDDC7.tmp"
["error"]=>
int(0)
["size"]=>
int(8984)
}
["test_pdf"]=>
array(5) {
["name"]=>
string(10) "prueba.pdf"
["type"]=>
string(15) "application/pdf"
["tmp_name"]=>
string(24) "C :\ xampp\tmp\phpDDC8.tmp"
["error"]=>
int(0)
["size"]=>
int(180572)
}