我想建立一个允许我上传文件的网站,然后实时显示处理文件的进度。客户端将上传分隔文件。上传后,我将验证数据中的标题/列。如果这遵循给定的模板,那么我将一次处理一行,并希望在发生这种情况时向用户提供实时反馈。我知道PHP是在get请求时运行的,因此我认为这种方法需要ajax来实现我的目标。我该如何去做这样的事情。任何伪代码、示例或教程将不胜感激。
我过去做过类似的事情。你需要做什么,开始文件处理。在设定的时间间隔(如一秒)之后,可以使用 ajax 调用轮询服务器以查看处理文件的进度。
顺便说一下,我会推荐jqueryUI进度条作为进度条,并为上传者上传。
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);
}
});