我找到了一个带有进度条的不错的上传脚本。但不幸的是,它对我不起作用:(我总是收到一个错误:未定义的索引:第 7 行 C:\wamp\www\upload\upload_parser.php 中的 file1。我已经花了几个小时来摆脱它成功。但是脚本应该可以工作。有人知道我为什么会收到这个错误吗?(仅供参考:我从http://www.developphp.com/view.php?tid=1351下载了它)我已经检查了 php。 ini。$_Files 的 print_r 向我显示了一个空错误。我读到一些关于无法通过 Ajax 上传图像的信息。但是该脚本适用于其他用户。完全奇怪。
我感谢每一个提示。非常感谢您。
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<script>
function _(el) {
return document.getElementById(el);
}
function uploadFile() {
var file = _("file1").files[0];
alert (file.name+" - "+file.size+" - "+file.type);
var formdata = new FormData();
formdata.append("file1", file);
var ajax = new XMLHttpRequest();
ajax.upload.addEventListener("progress", progressHandler, false);
ajax.addEventListener("load", completeHandler, false);
ajax.addEventListener("error", errorHandler, false);
ajax.addEventListener("abort", abortHandler, false);
ajax.open("POST", "upload_parser.php");
ajax.send();
}
function progressHandler (event) {
_("loaded_n_total").innerHTML = "Uploaded "+event.loaded+" bytes of "+event.total;
var percent = (event.loaded / event.total) * 100;
_("progressBar").value = Math.round(percent);
_("status").innerHTML = Math.round(percent) +"% uploaded... please wait";
}
function completeHandler (event) {
_("status").innerHTML = event.target.responseText;
_("progressBar").value = 0;
}
function errorHandler (event) {
_("status").innerHTML = "Upload Failed";
}
function abortHandler (event) {
_("status").innerHTML = "Upload Aborted";
}
</script>
</head>
<body>
<div>File Upload</div>
<form id="upload_form" enctype="multipart/form-data" method="post">
<input type="file" name="file1" id="file1"><br>
<input type="button" value="Upload File" onclick="uploadFile()">
<progress id="progressBar" value="0" max="100" style="width: 300px;"></progress>
<h3 id="status"></h3>
<p id="loaded_n_total"></p>
</form>
</body>
</html>
和 upload_parser.php 文件:
<?php
print_r($_FILES);
$fileName = $_FILES["file1"]["name"];
$fileTmpLoc = $_FILES["file1"]["tmp_name"];
$fileType = $_FILES["file1"]["type"];
$fileSize = $_FILES["file1"]["size"];
$fileErrorMsg = $_FILES["file1"]["error"];
if (!$fileTmpLoc) {
echo "ERROR! Please browse for a file before clicking the upload button.";
exit();
}
if (move_uploaded_file($fileTmpLoc, "uploads/$fileName")) {
echo "$fileName upload is complete";
}
else {
echo "move_uploaded_file function failed";
}
?>