[英]Uploading and processing a file with PHP and Ajax
我想建立一個允許我上傳文件的站點,然后顯示實時處理文件的進度。 客戶端將上傳定界文件。 上傳后,我將驗證數據中的標題/列。 如果這遵循給定的模板,那么我將一次處理一行,並希望在發生這種情況時將實時反饋給用戶。 我知道在獲取請求時就運行了PHP,因此我認為該方法將需要ajax來實現我的目標。 我該如何做這樣的事情。 任何偽代碼,示例或教程將不勝感激。
我過去做過類似的事情。 您需要執行的操作開始文件處理。 在設置的時間間隔(例如一秒鍾)之后,可以使用ajax調用來輪詢服務器以查看處理文件的進度。
順便說一句,我會建議jQueryUI的進度的進度條和uploadify的上傳。
Javascript :
setTimeout("UpdateProgressBar();", 1000);
$.ajax({
type: "POST",
async: true,
data: // params go here
url: // path to web method
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
// Do stuff when file is finished processing
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
if (errorThrown != null)
alert(textStatus + " : " + errorThrown);
}
});
function UpdateProgressBar() {
$.ajax({
type: "POST",
async: true,
data: // my params go here
url: // path to web method
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
var totalRecords = result.d.TotalRecords;
var recordsProcessed = result.d.RecordsProcessed;
var percentProcessed = pagesProcessed / totalRecords;
$('#div_ProgressBar').progressbar("value", percentProcessed);
if (recordsProcessed < totalRecords)
setTimeout("UpdateProgressBar();", 800);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
if (errorThrown != null)
alert(textStatus + " : " + errorThrown);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.