How to download (stream) large files in symfony2

后端 未结 1 321
我寻月下人不归
我寻月下人不归 2021-01-19 02:26

What I want to do :
User hit a url (ex: BASE-URL/download/json).
Code fetch huge data from database and stream it into a JSON file and user is promp

相关标签:
1条回答
  • 2021-01-19 03:09

    I was able to achieve what I wanted with the help of all the above comments.
    Here is the code for the same.

    $response = new StreamedResponse();  
    $i = -999999;  
    $response->setCallback(function () {  
        while($i < 999999){  
            echo '{"G1ello":"hualala"}';  
            $i = $i + 1;  
        }  
    });  
    $filename = "Data.json";  
    $contentDisposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $filename);
    $response->headers->set('Content-Type', 'application/json');
    $response->headers->set('Content-Disposition', $contentDisposition);
    return $response;
    

    It created a large file which I was able to download. Exactly what I wanted. Posting the solution so that it can help someone with same issue.

    0 讨论(0)
提交回复
热议问题