我试图让一个 ajax 文件上传器工作,并将一些额外的表单数据传递给 php。php 脚本能够获取 $_FILES['fileToUpload'] 但我得到了 $_POST["fname"]; 的未定义索引;
PHP
if(isset($_FILES['fileToUpload'])){
$fname = $_POST["fname"];
$name = htmlentities($_FILES['fileToUpload']['name']);
}
JAVASCRIPT
var fd = new FormData();
fd.append("fileToUpload", document.getElementById('fileToUpload').files[0]);
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", uploadProgress, false);
xhr.addEventListener("load", uploadComplete, false);
xhr.addEventListener("error", uploadFailed, false);
xhr.addEventListener("abort", uploadCanceled, false);
xhr.open("POST", "uploader.php");
xhr.send(fd);
HTML
<form id="form1" name="form1" enctype="multipart/form-data">
<div id="user">
<input type="text" id="fname" name="fname" />
</div>
<div id="fileInfo">
<div id="fileName"></div>
<div id="fileSize"></div>
<div id="fileType"></div>
</div>
<div class="row"></div>
<div class="row">
<input type="file" name="fileToUpload" id="fileToUpload" onchange="fileSelected();"/>
</div>
<div class="row">
<input type="button" onclick="uploadFile()" value="Upload" />