我有这个html,需要将文件上传到服务器,然后用另一个php处理它,最后使文件在html div上可用,我正在阅读ajax成功调用,我可以嵌套每个函数,但是作为jquery和ajax函数的新手,我真的不知道如何使这个工作...在下面的例子中我只能用按钮调用函数,而不是ajax成功调用...
<html>
<head>
</head>
<body>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
function uploadFile(){
var file = document.getElementById("file").files[0];
// alert(file.name+" | "+file.size+" | "+file.type);
var formdata = new FormData();
formdata.append("file", 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.php");
ajax.send(formdata);
}
function progressHandler(event){
document.getElementById("loaded_n_total").innerHTML = "Uploaded "+event.loaded+" bytes of "+event.total;
var percent = (event.loaded / event.total) * 100;
document.getElementById("progressBar").value = Math.round(percent);
document.getElementById("status").innerHTML = Math.round(percent)+"% uploaded... please wait";
}
function completeHandler(event){
document.getElementById("status").innerHTML = event.target.responseText;
document.getElementById("progressBar").value = 0;
}
function errorHandler(event){
document.getElementById("status").innerHTML = "Upload Failed";
}
function abortHandler(event){
document.getElementById("status").innerHTML = "Upload Aborted";
}
function drive(){
var driver= new XMLHttpRequest();
driver.open("GET","drive.php",true);
driver.onreadystatechange= function(){
if(driver.readyState== 4 && driver.status == 200)
{
document.getElementById("response").innerHTML = driver.responseText;
}
}
driver.send();
}
function download(){
var download= new XMLHttpRequest();
download.open("GET","download.php",true);
download.onreadystatechange= function(){
if(download.readyState== 4 && download.status == 200)
{
document.getElementById("download").innerHTML = download.responseText;
}
}
download.send();
}
$.fn.YourName=function(){
uploadFile();
}
$.fn.drive=function(){
drive();
}
$(document).ready(function(){
$("#upload").click(function(){
uploadFile();
$.ajax({url: "upload.php", success: function(result){
$.ajax({url: "drive.php", success: function(result){
download();
}});
}});
});
});
</script>
<form id="upload_form" enctype="multipart/form-data" method="post">
<input type="file" name="file" id="file"><br>
<input type="button" id="upload" value="upload File">
<progress id="progressBar" value="0" max="100" style="width:300px;"></progress>
<h3 id="status"></h3>
<p id="loaded_n_total"></p>
</form>
<div id="response"></div>
<div id="download"></div>
</body>
</html>
答案 0 :(得分:0)
$(document).ready(function(){
$("#upload").click(function(){
uploadFile();
$.ajax({url: "upload.php", success: function(result){
$.ajax({url: "drive.php", success: function(result){
download();
}});
}});
});
});