返回文件流官方說(shuō)明是這個(gè)
<?php
namespace app\controller;
use support\Request;
class FooController
{
public function hello(Request $request)
{
return response()->file(public_path() . '/favicon.ico');
}
}
webman支持發(fā)送超大文件
對(duì)于大文件(超過(guò)2M),webman不會(huì)將整個(gè)文件一次性讀入內(nèi)存,而是在合適的時(shí)機(jī)分段讀取文件并發(fā)送
webman會(huì)根據(jù)客戶端接收速度來(lái)優(yōu)化文件讀取發(fā)送速度,保證最快速發(fā)送文件的同時(shí)將內(nèi)存占用減少到最低
數(shù)據(jù)發(fā)送是非阻塞的,不會(huì)影響其它請(qǐng)求處理
file方法會(huì)自動(dòng)添加if-modified-since頭并在下一個(gè)請(qǐng)求時(shí)檢測(cè)if-modified-since頭,如果文件未修改則直接返回304以便節(jié)省帶寬
發(fā)送的文件會(huì)自動(dòng)使用合適的Content-Type頭發(fā)送給瀏覽器
如果文件不存在,會(huì)自動(dòng)轉(zhuǎn)為404響應(yīng)
如果我是要請(qǐng)求一個(gè)遠(yuǎn)程url,他會(huì)驗(yàn)證我的秘鑰,然后返回一個(gè)文件流給我,我怎么返回到前端去呢,對(duì)方既不給文件名,也不返文件長(zhǎng)度給我,就一個(gè)文件流,還不能Range斷點(diǎn)續(xù)傳。
我之前是先下載到webman服務(wù)器本地,存起來(lái),再返給前端,但是這樣大文件超過(guò)2.1GB請(qǐng)求會(huì)直接報(bào)錯(cuò),也不知道是哪里設(shè)置的緩存太小還是怎么,報(bào)錯(cuò)如下:
cURL error 18: transfer closed with outstanding read data remaining (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) ;
我能否直接轉(zhuǎn)發(fā)這個(gè)文件流到前端去,而不再轉(zhuǎn)存到本地,這種4個(gè)GB的文件能正常轉(zhuǎn)發(fā)嗎?轉(zhuǎn)發(fā)怎么寫呢,curl獲取文件流,然后 $result = curl_exec($ch),再return $result嗎?
之前用guzzle下載是這樣寫的:
$pdfFileResource = fopen($file_path_and_name, 'w+');
$httpClient = new Client();
$response = $httpClient->get($url,[RequestOptions::SINK => $pdfFileResource,'headers'=>$headers,'timeout'=>0]);
<?php
namespace app\controller;
use support\Request;
use Workerman\Http\Client;
use Workerman\Protocols\Http\Chunk;
class DownloadController
{
public function index(Request $request)
{
$connection = $request->connection;
$http = new Client();
$http->request('https://example.com/xxx.pdf', [
'method' => 'GET',
'progress' => function($buffer) use ($connection) {
$connection->send(new Chunk($buffer));
},
'success' => function($response) use ($connection) {
$connection->send(new Chunk('')); // 發(fā)送空的的chunk代表response結(jié)束
},
]);
return response()->withHeaders([
"Content-Type" => "application/pdf",
"Transfer-Encoding" => "chunked",
]);
}
}
代碼類似這樣。
文檔參考 http://www.wtbis.cn/doc/workerman/components/workerman-http-client.html
public function downloadFile(Request $request)
{
$url = "https://api.aliyun.com/api/downfile;
$headers = [
"APPID"=>config('static.APPID'),
"APPKEY"=>config('static.APPKEY'),
];
$connection = $request->connection;
$http = new \Workerman\Http\Client();
$http->request($url, [
'method' => 'GET',
'progress' => function($buffer) use ($connection) {
$connection->send(new Chunk($buffer));
},
'success' => function($response) use ($connection) {
$connection->send(new Chunk('')); // 發(fā)送空的的chunk代表response結(jié)束
},
'headers' => $headers,
]);
return response()->withHeaders([
"Transfer-Encoding" => "chunked",
]);
}
這樣寫沒用,前端點(diǎn)這個(gè)按鈕沒反應(yīng)
6,那如果是指定部分的文件流呢,比如一個(gè)mp4文件,客戶端來(lái)讀取時(shí),我取其它網(wǎng)站讀取流來(lái)返回,mp4又可能跳躍到其他位置
對(duì)的,需求就是下載這個(gè)文件,我獲取一個(gè)文件id,然后http請(qǐng)求一個(gè)遠(yuǎn)程地址,通過(guò)API,他會(huì)返一個(gè)文件流給我,但是有4個(gè)G,我之前都是直接下載到服務(wù)器,這個(gè)4GB的文件,我下不了,每次一到2.1G左右都報(bào)錯(cuò) “cURL error 18: transfer closed with outstanding read data remaining” ,所以我才想能不能直接轉(zhuǎn)發(fā)這個(gè)流給前端,但是有點(diǎn)不好處理,網(wǎng)上的案例都是遠(yuǎn)程地址是個(gè) xxx.com/xxx/xxx.pdf ,這種資源文件地址,但我這個(gè)其實(shí)是個(gè)API接口,他通過(guò)驗(yàn)證才會(huì)返文件流