我正在尝试使用 Ajax 上传文件,但在处理文件时遇到了麻烦...出于测试目的,我构建了一个如下所示的简单代码:
JS:
xmlhttp=new XMLHttpRequest();
xmlhttp.open("POST",document.getElementById('upload').action,true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
var cmdStr="q="+str;
xmlhttp.send(cmdStr);
document.getElementById("ResponseDiv").innerHTML=xmlhttp.responseText;
PHP:
$q=$_POST["q"];
echo $q;
它工作正常并xmlhttp.responseText
打印[object File]
。
但是,我的问题是我需要使用$_FILES["q"]['tmp_name']
. 为此,我已将代码更改为以下内容:
JS:
xmlhttp=new XMLHttpRequest();
xmlhttp.open("POST",document.getElementById('upload').action,true);
xmlhttp.setRequestHeader("enctype","multipart/form-data");
var cmdStr="q="+str;
xmlhttp.send(cmdStr);
document.getElementById("ResponseDiv").innerHTML=xmlhttp.responseText;
PHP:
$q=$_FILES["q"]["tmp_name"];
echo $q;
问题是现在xmlhttp.responseText
我什么也得不到。有人知道我在做什么错吗?