我正在尝试通过ajax上传.doc, .docx
文件,但它一直在我的控制台中向我发送错误。这是我的剧本。
var file = $('#file').val();
$(document).on('change','#file',function(){
var data = new FormData($("#fileinfo"));
$.each($('#file')[0].files, function(i, file) {
data.append('file-'+i, file);
});
console.log(FormData);
$.ajax({
url: "main-function.php?call=upload",
data: data,
cache: false,
contentType: false,
processData: false,
type: 'POST',
success: function(data){
console.log(data)
}
});
return false;
});
这是main-function.php
:
if ($_FILES['file']['error'] !== UPLOAD_ERR_OK) {
die("Upload failed with error " . $_FILES['file']['error']);
}
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $_FILES['file']['tmp_name']);
$ok = false;
$userid = $_SESSION['username'];
switch ($mime) {
case 'application/pdf':
case 'application/msword':
$ok = true;
move_uploaded_file($_FILES["file"]["tmp_name"],
"pds/" . $userid . '-' . time().'-' . $_FILES["file"]["name"]);
break;
default:
echo 'Type not supported';
}
编辑:这是错误
<b>Notice</b>: Undefined index: file in <b>C:\xampp\htdocs\hrms\main\main-function.php</b> on line <b>9</b><br />
<b>Warning</b>: finfo_file() [<a href='function.finfo-file'>function.finfo-file</a>]: Empty filename or path in <b>C:\xampp\htdocs\hrms\main\main-function.php</b> on line <b>9</b><br />
答案 0 :(得分:0)
您发送的文件为file-0
,file-1
,...但您正在寻找file
。
如果您只发送一个文件,请将文件发送为file
data.append('file', $('#file')[0].files[0]);
如果表单是#fileinfo
,你还是要将它传递给FormData构造函数而不是jQuery对象
var data = new FormData($("#fileinfo")[0]);
如果#file
处于该格式,您将两次发送文件。