Skip to content

Commit 552ca1d

Browse files
gmtaJelle Raaijmakers
andauthored
Make exporters provide a MIME type (#319)
This allows us to set a correct `Content-Type` header in the `BinaryFileResponse`. If we don't do this, the user is required to have automatic MIME type detection available, such as provided by `symfony/mime`. Co-authored-by: Jelle Raaijmakers <[email protected]>
1 parent 01cec9a commit 552ca1d

File tree

5 files changed

+21
-0
lines changed

5 files changed

+21
-0
lines changed

src/Exporter/Csv/CsvExporter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public function export(array $columnNames, \Iterator $data): \SplFileInfo
4040
return new \SplFileInfo($filePath);
4141
}
4242

43+
public function getMimeType(): string
44+
{
45+
return 'text/csv';
46+
}
47+
4348
public function getName(): string
4449
{
4550
return 'csv';

src/Exporter/DataTableExporterInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ interface DataTableExporterInterface
2626
*/
2727
public function export(array $columnNames, \Iterator $data): \SplFileInfo;
2828

29+
/**
30+
* The MIME type of the exported file.
31+
*/
32+
public function getMimeType(): string;
33+
2934
/**
3035
* A unique name to identify the exporter.
3136
*/

src/Exporter/DataTableExporterManager.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public function getResponse(): Response
6969

7070
$response = new BinaryFileResponse($file);
7171
$response->deleteFileAfterSend(true);
72+
$response->headers->set('Content-Type', $exporter->getMimeType());
7273
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $response->getFile()->getFilename());
7374

7475
$this->dataTable->getEventDispatcher()->dispatch(new DataTableExporterResponseEvent($response), DataTableExporterEvents::PRE_RESPONSE);

src/Exporter/Excel/ExcelExporter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ private function autoSizeColumnWidth(Worksheet $sheet): void
6767
}
6868
}
6969

70+
public function getMimeType(): string
71+
{
72+
return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
73+
}
74+
7075
public function getName(): string
7176
{
7277
return 'excel';

tests/Fixtures/AppBundle/DataTable/Exporter/TxtExporter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ public function export(array $columnNames, \Iterator $data): \SplFileInfo
3535
return new \SplFileInfo($filename);
3636
}
3737

38+
public function getMimeType(): string
39+
{
40+
return 'text/plain';
41+
}
42+
3843
public function getName(): string
3944
{
4045
return 'txt';

0 commit comments

Comments
 (0)