Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过ajax将表单文件发送到php_Php_Html_Ajax - Fatal编程技术网

通过ajax将表单文件发送到php

通过ajax将表单文件发送到php,php,html,ajax,Php,Html,Ajax,我想通过ajax将文件发送到php,我有下面的代码,但当我运行它时,它会显示未定义的索引:thefile 有什么问题? HTML 及表格: <form action="#" > <input type="file" name="thefile" id="thefile"/> <input type="button" name="Send" value="send" onclick="postData('upLoad.php');"/> </form&

我想通过ajax将文件发送到php,我有下面的代码,但当我运行它时,它会显示未定义的索引:thefile 有什么问题? HTML

及表格:

<form action="#" >
<input type="file" name="thefile" id="thefile"/>
<input type="button" name="Send"  value="send" onclick="postData('upLoad.php');"/>  </form>

这适用于有多个文件的情况,但也适用于一个文件:

var formData = new FormData();
    jQuery.each($('#thefile')[0].files, function(i, file) {
    formData.append('thefile-'+i, file);
});
第一个文件现在可以在以下位置找到:

$_FILES['thefile-0']

还要确保设置内容类型。

$\u文件
必须为空。我在任何地方都看不到
multipart/form data
。@PLB您能给我解释一下吗?添加
xmlHttp.setRequestHeader(“内容类型”,“multipart/form data”)
在onreadystatechange之前,但它没有解决问题
var formData = new FormData();
    jQuery.each($('#thefile')[0].files, function(i, file) {
    formData.append('thefile-'+i, file);
});
$_FILES['thefile-0']