1

我想建立一个允许我上传文件的网站,然后实时显示处理文件的进度。客户端将上传一个分隔文件。上传后,我将验证数据中的标题/列。如果这遵循给定的模板,那么我将一次处理一行,并希望在发生这种情况时向用户提供实时反馈。我知道 PHP 是在获取请求时运行的,因此我认为这种方法需要 ajax 来实现我的目标。我该怎么做这样的事情。任何伪代码、示例或教程将不胜感激。

4

1 回答 1

1

我过去做过类似的事情。你需要做的是开始文件处理。在设定的时间间隔(例如一秒)之后,您可以使用 ajax 调用轮询服务器以查看处理文件的进度。

顺便说一句,我会推荐jqueryUI progressbar作为进度条,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);
        }
    });
于 2012-06-07T02:45:26.907 回答