dongzhang5787 2014-05-10 09:27
浏览 33
已采纳

从Slim Framework下载文件时出现问题

I want to download file through Slim Framework (The reason why I'm using Slim Framework is because I want to write a simple REST API). I found this post: Download file from Slim Framework 2.4 and this post: http://help.slimframework.com/discussions/questions/359-file-download. I followed the method. Here is my code:

$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);

});

I'm using the Google Chrome's Advanced Rest Client application to test. The problem is the browser hung after the file downloaded.If I comments out the NOTE part in my source code, the browser won't hang again. But I got a "Unexpected token P" error.

Can anybody help? Thanks.

  • 写回答

4条回答 默认 最新

  • dongxingchang9345 2014-05-12 18:39
    关注

    Try the solution here: PHP readfile() adding extra bytes to downloaded file.

    You lack the calls to ob_clean, flush and exit.

    The problem might be becasue of extra characters output with the rest of the file contents.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制