简体   繁体   中英

How to download a csv file with symfony 4?

I'm trying to download a csv file with symfony 4. I read the documentation. I understood that I must normalize my objects and encode in csv.

My controller:

/**
* @Route("/export.csv", name="exportcsv", options={"expose"=true}, methods={"GET","POST"})
*
**/
public function exportcsv(){
   $sessionuser = $this->session->get('user');
   $user = $this->finduser($sessionuser);
   $datas = $this->getDoctrine()->getRepository(Object::class)-> findBy(['Event'=> $user->getEvent()]);
   $encoders = [new CsvEncoder()];
   $normalizers = array(new ObjectNormalizer());
   $serializer = new Serializer($normalizers, $encoders);
   $csvContent = $serializer->serialize($datas, 'csv'); 
   return new Response($csvContent);
}

When i try to go on this route, i can see in my browser all my datas in the good format for a csv. For example, I have:

NameColumn1, NameColumn2, NameColumn3

Data1Column1, Data1Column2, Data1Column3

Data2Column1, Data2Column2, Data2Column3

I read that it's the good format to create a csv file.

I would like to know I to generate the csv file.

If anyone have a solution, it would be awesome. I'm on it for a week and I'm totally lost.

Thanks.

EDIT: By the way I tried this solution How to force download a .csv file in Symfony 2, using Response object? It download a file but it's empty.

I would go for something like this:

How to create a link that download a file in Symfony

But for this to work you need to put the content into an temporary csv file.

Hope this helps, stay safe!

I found the solution.

In fact, i had 2 problems: The first was I should set the headers in the correct format. The second was Visual studio code who didn't read the code between 2 commented sections of code.

For information, this is my new code. If it can help anybody ;)

 /**
 * @Route("/export.csv", name="exportcsv", options={"expose"=true}, methods={"GET","POST"})
 *
 **/
public function exportcsv(){
  $sessionuser = $this->session->get('user');
  $user = $this->finduser($sessionuser);
  //search all the datas of type Object
  $datas = $this->getDoctrine()->getRepository(Object::class)-> findBy(['Event'=> $user->getEvent()]);
  // normalization and encoding of $datas
  $encoders = [new CsvEncoder()];
  $normalizers = array(new ObjectNormalizer());
  $serializer = new Serializer($normalizers, $encoders);
  $csvContent = $serializer->serialize($datas, 'csv');

  $response = new Response($csvContent);
  $response->headers->set('Content-Encoding', 'UTF-8');
  $response->headers->set('Content-Type', 'text/csv; charset=UTF-8');
  $response->headers->set('Content-Disposition', 'attachment; filename=sample.csv');
  return $response;
}

Thanks to everyone for your help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM