webman 控制器返回远程服务器的文件流

final507

问题描述

webman 控制器返回远程服务器的文件流,参考了帖子
https://www.workerman.net/q/13212

程序代码或配置

public function streamFile()
    {
        $http = new \Workerman\Http\Client();
        $url ='https://xxxx.cn/uploads/default/2024-10/09/670629b1d8f8a.jpeg';
        $connection = \request()->connection;
        // $http = new Client();
        $http->request($url, [
            'method' => 'GET',
            'progress' => function($buffer) use ($connection) {
            print_r($buffer);
                $connection->send(new \Workerman\Protocols\Http\Chunk($buffer));
            },
            'success' => function($response) use ($connection) {
                $connection->send(new \Workerman\Protocols\Http\Chunk('')); // 发送空的的chunk代表response结束
            },
        ]);
        $downloadName = uniqid().'.jpeg';
        return response()->withHeaders([
            "Content-Type" => "application/jpeg",
            "Transfer-Encoding" => "chunked",
            "Content-Disposition"=>"attachment;filename=\"$downloadName\""
        ]);
    }

请求后下载的是一个空文件,
progress 回调内也是可以读取到文件的二进制内容的,

操作系统环境及workerman/webman等具体版本

win10 调试
PHP 8.0.2
"workerman/http-client": "^2.2",
"workerman/webman-framework": "^1.5",
本地调试,没有使用nginx 作为反向代理

288 2 1
2个回答

walkor 打赏

workerman版本贴下

小W

Content-Type ?

  • final507 2024-11-11

    这个删除了,还是不行,不过我重新安装了一下 composer.json,代码就正常了

×
🔝