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 :
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)