首页 > 解决方案 > Slim 3 Framework 下载文件不显示下载对话框

问题描述

我尝试下载文件 .png 使用带有此代码的 Slim 3:

$app->get('/api/download/content/image/{filename}', function($request, Slim\Http\Response $response, $args) {
$file = __DIR__ . '/uploads/content/image/'. $args['filename'];
$fh = fopen($file, 'rb');

$stream = new \Slim\Http\Stream($fh); // create a stream instance for the response body

return $response->withHeader('Content-Type', 'application/force-download')
    ->withHeader('Content-Type', 'application/octet-stream')
    ->withHeader('Content-Type', 'application/download')
    ->withHeader('Content-Description', 'File Transfer')
    ->withHeader('Content-Transfer-Encoding', 'binary')
    ->withHeader('Content-Disposition', 'attachment; filename="' . basename($file) . '"')
    ->withHeader('Expires', '0')
    ->withHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0')
    ->withHeader('Pragma', 'public')
    ->withBody($stream); });

但下载对话框不显示。只显示字符串

苗条的结果

标签: phpfiledownloadslim-3

解决方案


推荐阅读