incorrect csv file name after download from controller

Viewed 113
  1. What needs to be changed so that the name of the file after downloading is the same as I indicated

  2. Why the file type is not detected on Windows and Linux. (on macos the extension is defined)

I do not save the file on the server, but I generate data on the fly and send it to the new Response() After downloading, the file does not have the name that I want, it also does not show the file extension on Windows

    public function download(Request $request): Response
    {
        $array = [];

       /// filling the array

        $serializer = new Serializer([new ObjectNormalizer()], [new CsvEncoder()]);
        $csv = $serializer->encode($array, 'csv', [CsvEncoder::DELIMITER_KEY => ';', CsvEncoder::OUTPUT_UTF8_BOM_KEY => true]);

        $response = new Response($csv);
        $disposition = HeaderUtils::makeDisposition(HeaderUtils::DISPOSITION_ATTACHMENT, "desiredFilename.csv");
        $response->headers->set('Content-Type', 'text/csv; charset=UTF-8');
        $response->headers->set('Content-Disposition', $disposition);

        return $response;
    }

file name after downloading like e9ddc45c-f3b6-404d-8251-ee51bc488c6e.csv

2 Answers

regarding The MDN Doc you need to include your filename in the double quotations filename="desiredFilename.csv" .

I think maybe you have to wrap the file name into it:

        $disposition = HeaderUtils::makeDisposition(HeaderUtils::DISPOSITION_ATTACHMENT, '"desiredFilename.csv"');

FYI: I have seen the Symphony Docs and it's example is like what you have provided.

You can try this :

$response->header->set('Content-Disposition: attachment; filename="desiredFilename.csv"');

I think this link can be useful: Symfony Docs

Enjoy !

Related