如果標題有誤導性,我很抱歉,沒有簡單的方法可以減少標題中的問題。我目前正在嘗試使用AJAX文件上傳;我之前做過標準文件上傳,但現在我試圖通過添加跟蹤文件上傳進度的進度條並在視頻完成上傳,完成處理並將其放入數據庫時通知用戶,使我的應用程序界面更好一些。告訴AJAX請求已經完成,但沒有取消文件上傳
它工作得很好,除了Ajax目前正在爲我的腳本完成執行。基本上,發生的情況是大多數呼叫都來自一個名爲uploadfiles.php
的文件。我正在使用一個帶有feof
的while循環,並將進度記錄到一個文件中,然後該文件應該由另一個AJAX提取,該文件循環直到文件告訴它該請求已更改/完成。
但是,再說一次,進度條不動如因某種原因第一個AJAX請求等到uploadfiles.php
完全執行完畢(當文件完成上傳,處理和被感動了,會使這將是進度條毫無意義),並且由於這不會讓下一個AJAX請求檢索日誌文件的內容。我曾嘗試以下:
- 難道
ob_start(); and ob_end_flush()
文件是從PHP的tmp文件夾移動到我的自定義TMP文件夾 - 難道
flush()
,我想是完全一樣的事情如上 - 點剛過呼應隨機的東西,並再次刷新,希望事情會發生
我還添加ignore_user_abort()
,以確保該請求,如果用戶離開頁面不會被中止/請求結束或中止得到。
**這裏的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
常數
在此先感謝。
郵政相關的代碼在你的問題。 – Mike
這是相當長的代碼,你確定嗎?@Mike – CristianHG
我添加了pastebin鏈接 – CristianHG