国产+高潮+在线,国产 av 仑乱内谢,www国产亚洲精品久久,51国产偷自视频区视频,成人午夜精品网站在线观看

webman如何通過url獲取文件流直接轉(zhuǎn)發(fā)給客戶端?

RainLee1990

問題描述

根據(jù)url直接獲取文件流發(fā)送給客戶端,服務(wù)器本地不做存儲,當(dāng)前參考文檔 http://www.wtbis.cn/doc/workerman/components/workerman-http-client.html 寫的代碼文件時下載下來了但是報文件是損壞的打不開,一下是具體代碼

public function download(Request $request): \support\Response
{
    $url = $request->get('url');
    if (empty($url)) {
        return response_json(400, '參數(shù)不能為空');
    }
    $headers = get_headers($url, 1);

    if (!$headers || !str_contains($headers[0], '200')) {
        return response_json(400, 'URL不可訪問', null, 403);
    }

    try {
        $connection = $request->connection;
        $http = new 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é)束
            },
        ]);
    } catch (\Throwable $e) {
        return response_json(500, $e->getMessage());
    }
    return response()->withHeaders([
        "Content-Type" => $headers['Content-Type'],
        "Transfer-Encoding" => "chunked",
    ]);
}
304 1 0
1個回答

超高級的稻姬

不懂就問,為什么這個url不直接給前端下載

  • 大古 2025-04-28

    "Content-Type" => $headers['Content-Type'],

  • RainLee1990 2025-04-29

    因為要在小程序用,域名太多小程序的下載域名填寫不全

  • 超高級的稻姬 2025-04-30

    你是否在搜索nginx代理,搜索nginx proxy_pass可以解決你的大部分問題

??