告诉 AJAX 请求已完成而不取消文件上传

Tell AJAX that the request was completed without canceling file upload

如果标题误导了我,我很抱歉,没有简单的方法可以在标题中删减我的问题。我目前正在试验 AJAX 文件上传;我以前做过标准的文件上传,但现在我试图通过添加一个进度条来使我的应用程序的界面更好一些,该进度条跟踪文件上传进度并在视频完成上传、完成处理并放入数据库时​​通知用户。

除了 Ajax 当前正在为我的脚本完成执行而工作之外,它大部分工作正常。基本上,大多数调用都是从一个名为 uploadfiles.php 的文件发出的。我正在使用带有 feof 的 while 循环并将进度记录到一个文件,然后该文件应该由另一个 AJAX 循环获取,直到文件告诉它请求有 changed/completed .

但是,由于某种原因第一个 AJAX 请求正在等待直到 uploadfiles.php 完全完成执行 (这将是文件完成上传、处理和移动,会使进度条变得毫无意义),因此不会让下一个 AJAX 请求检索日志文件内容。我尝试了以下方法:

我还添加了 ignore_user_abort() 以确保如果用户离开 page/the 请求结束或被中止,请求不会被中止。

**这是JS代码:

    function uploadFiles()
{
    var data2 = new FormData($("#specialform")[0]);

    timestamp = new Date().getUTCMilliseconds();
    timestamp = timestamp.toString();

    data2.append('outputId', timestamp);

    console.log(data2);

    $.ajax({
        type        : "POST",
        url         : "actions/uploadfiles.php",
        data        : data2,
        processData : false,
        contentType : false,
        success: function(data)
        {
            alert('Finished first request');
            getLog();
        },
        error: function (xhr, ajaxOptions, thrownError)
        {
            alert(xhr.responseText);
            alert(thrownError);
        },
        xhr: function ()
        {
            var xhr = new window.XMLHttpRequest();

            xhr.addEventListener("progress", function (evt)
            {
                if (evt.lengthComputable)
                {
                        var percentComplete = evt.loaded / evt.total;
                        console.log(percentComplete);
                        var percentComplete = percentComplete * 100;

                        $("#progressBar").css({ width : percentComplete + '%' });

                        if ( percentComplete >= 100 )
                            xhr.abort();
                }
                else
                        console.log('unable to complete');
            }, false);

            return xhr;
        },
    })
    .fail(function(data)
    {
        console.log(data);
    });

    //return {'error' : 'No files', 'result' : null};
}

function getLog()
{
    if ( finished == false )
    {
        console.log("logs/" + timestamp);

        $.ajax({
            type        : "GET",
            url         : "logs/" + timestamp,
            processData : false,
            contentType : false,
            success: function(data)
            {
                if ( data == "processing" && !processed )
                {
                    alert('processing video');

                    $("#progressBar").css({ 'background-color' : 'yellow' });

                    processed = true;
                }

                if ( data == "done" )
                {
                    alert('finished conversion');

                    $("#progressBar").css({ 'background-color' : 'green' });

                    finished = true;
                }

                if ( $.isNumeric(data) )
                    $("#progressBar").css({ width : data + '%' });

                console.log(data);
            }
        });

        setTimeout(getLog, 1000);
    }
}

这是 PHP 代码:

<?php

require '../Init.php';

define('TMP_PATH', PATH.'/tmp');
define('V_STORE', PATH.'/resources/videos');
define('FFMPEG_PATH', 'F:/Webserver/ffmpeg/bin/ffmpeg.exe');

// ...

ob_start();

echo "END";

ob_end_flush();
flush();

// ...

$remote = fopen($_FILES['file']['tmp_name'], 'r');
$local = fopen($filename, 'w');

$read_bytes = 0;

set_time_limit(28800000);
ignore_user_abort(true);

$interval = 0;

while( !feof($remote) )
{
    $buffer = fread($remote, 2048);
    fwrite($local, $buffer);

    $read_bytes += 2048;

    $progress = min(100, 100 * $read_bytes / $filesize);

    if ( $interval <= 0 )
    {
        file_put_contents('../logs/'.$logFile, $progress);

        $interval = 1000;
    }
    else
        $interval--;
}

// ...

$proc = popen(FFMPEG_PATH.' -i '.$filename.' -b '.$newBitrate.' '.V_STORE.'/'.$video_id.'.mp4', 'r');

while (!feof($proc))
{
    file_put_contents('../logs/'.$logFile, fread($proc, 4096));
}

file_put_contents('../logs/'.$logFile, "done");

sleep(5);

unlink('../logs/'.$logFile);

不用担心 Init.php 作为 (在这种情况下) 它仅用于 PATH 常量

提前致谢。

您是否在 Init.php 文件(或其他地方)中使用 PHP 基于文件的会话?默认情况下,这些文件会锁定,直到会话有效关闭,一次只允许执行一个请求。

如果是这样,您需要使用以下方法关闭上传文件中的会话:

session_write_close();