0

I'm trying to download a file created in stream to not have to write it in the disk. Here is what I do :

$response = new StreamedResponse(function() use ($csv_data) {
  // Open output stream
  $handle = fopen('php://output', 'w');
  foreach ($csv_data as $row) {
    fputcsv($handle, $row);
  }
  fclose($handle);

}, 200, [
  'Content-Type' => 'text/csv',
  'Content-Disposition'   => 'attachment; filename="legend.csv"',
]);


return $response;

Here is a screen of what I have in the network tab of chrome : enter image description here

I use the StreamedResponse according to the documentation but it's returning me the string of the values of my csvfile ("value1","value2", ...) but not the csvfile itself.

This method is called by an angular app.

What I am doing wrong ?

EDIT :

I try to make with a form :

        var form = angular.element('<form></form>');
        form.attr('action', 'http://myUrl/export-csv');
        form.attr('method', 'POST');
        var input = angular.element('<input></input>');
        input.attr('type', 'hidden');
        input.attr('name', 'data');
        input.attr('value', finalData);
        form.append(input);
        angular.element(document).find('body').append(form);
        form.submit().remove();

This will maybe works but I have some strange things, first the form redirect to the URL and not make a post request to it, when he redirect I have an ERR_INVALID_RESPONSE (but the url exist and the data is good => I test with postman and it works)

Spialdor
  • 1,475
  • 5
  • 20
  • 46
  • 1
    Apparently you need a [`Content-Disposition`](https://symfony.com/doc/current/components/http_foundation.html#serving-files) header. – Jerodev Aug 06 '18 at 15:16
  • Can you provide a bit more detail about the returned response? Maybe you can attach a screenshot or something to clearly spot what you want to point out. – alariva Aug 06 '18 at 15:17
  • Jerodev I try with this header but same result, alariva I add a screen – Spialdor Aug 06 '18 at 15:21
  • What exactly are you expecting here? It seems your response is valid. Are you using an AJAX request or something? – Devon Bessemer Aug 06 '18 at 15:25
  • I need that the response is a downloaded file, here I have just a text in the network tab. – Spialdor Aug 06 '18 at 15:29
  • Well, don't perform the request in AJAX if you want a download to occur.. – Devon Bessemer Aug 06 '18 at 15:30
  • I'm doing a post request, I use an angular app for the front and laravel for the backend, what can I do if I want to download ? .. – Spialdor Aug 06 '18 at 15:32
  • Not sure why you'd do a post request for a download, doesn't really fit into the definition of a POST, as POST is meant for creating/storing resources not retrieving them. AJAX requests aren't meant for downloading, they are meant for background requests. Do a normal form submission and it should work fine. – Devon Bessemer Aug 06 '18 at 15:44
  • Possible duplicate of [Handle file download from ajax post](https://stackoverflow.com/questions/16086162/handle-file-download-from-ajax-post) – Devon Bessemer Aug 06 '18 at 15:47
  • I edit with a try of your solution – Spialdor Aug 07 '18 at 07:35

0 Answers0