簡體   English   中英

ajax成功調用下一個函數

[英]ajax success to call next function

我有這個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>
$(document).ready(function(){
$("#upload").click(function(){

        uploadFile();

        $.ajax({url: "upload.php", success: function(result){

        $.ajax({url: "drive.php", success: function(result){
            download(); 

        }});

        }});
    });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM