場景如下:
我們有一個服務端,一個商戶端
服務端和商戶端的框架都是用的webman框架
服務端webman返回一個文件給商戶端,然后商戶端下載zip壓縮包進行解壓
并且商戶端下載zip文件的時候,讀取下載的進度并返回給前端顯示下載進度
服務端返回的下載文件有50M,可是商戶端下載的zip文件卻只有2M
商戶端是在webman的自定義進程中進行下載文件
然后用$connection對象實時輸出返回下載進度給前端顯示
不知道是因為什么導致的
/**
* 下載文件
*
* @Author 貴州猿創(chuàng)科技有限公司
* @Email 416716328@qq.com
* @DateTime 2023-03-25
* @param TcpConnection $connection
* @param array $data
* @param array $step
* @return void
*/
private function downloadFile(TcpConnection $connection, array $data, array $step)
{
$key = $data['key'];
$format = $data['format'];
// 獲取安裝文件
$host = HttpService::$host;
$url = "{$host}Plugin/install?key={$key}";
// header
$header = get_headers($url, true);
// 檢測文件大小
if (!isset($header['Content-Length'])) {
$this->sendRes($connection, [
'code' => 404,
'msg' => '插件ID錯誤',
], 'error');
return;
}
// 文件大小(字節(jié))
$fileSize = $header['Content-Length'];
// 遠程文件
$remote = fopen($url, 'rb');
if (!$remote) {
$this->sendRes($connection, [
'code' => 404,
'msg' => '遠程源文件錯誤',
], 'error');
return;
}
// 緩存文件路徑
$savePath = runtime_path("/temp.{$format}");
// 本地文件
$local = fopen($savePath, 'wb');
if (!$local) {
$this->sendRes($connection, [
'code' => 404,
'msg' => '本地源文件錯誤',
], 'error');
return;
}
// 每次寫入字節(jié)(1024=1kb)
$chunk = 4096;
// 分段讀取文件
$downlen = 0;
while (!feof($remote)) {
// 讀取流
$stream = fread($remote, $chunk);
// 寫入文件
fwrite($local, $stream, $chunk);
// 獲得塊大小
$downlen += strlen($stream);
// 計算百分比
$percent = round($downlen / $fileSize * 100, 2);
if ($percent < 100) {
$this->sendRes($connection, ['progress' => $percent]);
}
}
fclose($local);
fclose($remote);
}
請求接口沒有使用到nginx,是直接用的IP+端口號進行請求