【问题标题】:Issues when downloading file from Slim Framework从 Slim Framework 下载文件时的问题
【发布时间】:2016-07-14 06:05:40
【问题描述】:

我想通过 Slim Framework 下载文件(我使用 Slim Framework 的原因是因为我想编写一个简单的 REST API)。我找到了这篇文章: Download file from Slim Framework 2.4 和这篇文章:http://help.slimframework.com/discussions/questions/359-file-download。我按照方法。这是我的代码:

$app->get('/download/:id', 'authenticate', function($id) use ($app)
{    
    $log = '../download/'.$id.'/myfile.zip';

    $res = $app->response()->headers();
    $res['Content-Description'] = 'File Transfer';
    $res['Content-Type'] = 'application/octet-stream';
    $res['Content-Disposition'] ='attachment; filename=' . basename($log);
    $res['Content-Transfer-Encoding'] = 'binary';
    $res['Expires'] = '0';
    $res['Cache-Control'] = 'must-revalidate';
    $res['Pragma'] = 'public';
    $res['Content-Length'] = filesize($log);

    readfile($log);    

    // NOTE: 
//        $response = array();
//        $response["error"] = false;
//        echoRespnse(200, $response);

});

我正在使用 Google Chrome 的 Advanced Rest Client 应用程序进行测试。问题是下载文件后浏览器挂起。如果我在源代码中删除了 NOTE 部分,浏览器将不会再次挂起。但我收到“意外的令牌 P”错误。

有人可以帮忙吗?谢谢。

【问题讨论】:

    标签: php download slim


    【解决方案1】:

    在此处尝试解决方案:PHP readfile() adding extra bytes to downloaded file

    您缺少对 ob_clean、flush 和 exit 的调用。

    问题可能是由于文件内容的其余部分输出了额外的字符。

    【讨论】:

    • 我试试看。谢谢。
    【解决方案2】:

    对于 Slim 3,我有点挣扎。 我在 Slim 3.0 中遇到了这个问题。 我正在尝试类似以下的操作,并且 pdf 二进制文件显示在屏幕上。

    $header['Content-Description']  = 'File Transfer';
    $header['Content-Disposition'] = 'attachment; filename="' .basename("$file") . '"';
    
    // ...
    $response   = $response->withHeader($header);
    

    为了使其工作,您需要使用$response->withHeader() 设置键/值对。它就像一个魅力。例如:

    $response   = $response->withHeader('Content-Type', 'application/pdf');
    $response   = $response->withHeader('Content-Description', 'File Transfer');
    $response   = $response->withHeader('Content-Disposition', 'attachment; filename="' .basename("$file") . '"');
    $response   = $response->withHeader('Content-Transfer-Encoding', 'binary');
    $response   = $response->withHeader('Expires', '0');
    $response   = $response->withHeader('Cache-Control', 'must-revalidate');
    $response   = $response->withHeader('Pragma', 'public');
    $response   = $response->withHeader('Content-Length', filesize($file));
    

    【讨论】:

      【解决方案3】:

      也许有点晚了,但你可以使用这个包来响应苗条的 http 文件: https://github.com/mhndev/slim-file-response

      【讨论】:

        【解决方案4】:

        上述解决方案对您有用吗?这是我的代码,它只是将晦涩的字符直接输出到浏览器窗口中:

        $app->get('/file/:fileid', function($fileid) use ($app)
        {
            $zip = new ZipArchive();
            $zipname = "zipfile3.zip"; // Zip name
            $zip->open($zipname,  ZipArchive::CREATE);
            $files = array ( "sprecher_inserts_en_VB.doc" );
        
            foreach ($files as $file) {
                $path = "uploads/".$file;
                if(file_exists($path)){
                    $zip->addFromString(basename($path),  file_get_contents($path));  
                }
                else{
                    echo"file does not exist";
                }
            }
            $zip->close();
            // after that, the zip file is on the server and valid when downloaded manually (e.g. by entering its URL directly)
        
            $res = $app->response()->headers();
            $res['Content-Description'] = 'File Transfer';
            $res['Content-Type'] = 'application/octet-stream';
            $res['Content-Disposition'] ='attachment; filename=' . basename($zipname);
            $res['Content-Transfer-Encoding'] = 'binary';
            $res['Expires'] = '0';
            $res['Cache-Control'] = 'must-revalidate';
            $res['Pragma'] = 'public';
            $res['Content-Length'] = filesize($zipname);
        
            ob_clean();    
            flush();
            readfile($zipname);    
            exit();
        });
        

        浏览器窗口中的输出如下,没有加载任何下载文件的对话框:

        【讨论】:

          猜你喜欢
          • 2014-05-19
          • 1970-01-01
          • 2020-11-24
          • 2016-01-19
          • 1970-01-01
          • 2019-11-26
          • 2019-01-16
          • 2020-01-04
          • 1970-01-01
          相关资源
          最近更新 更多