diff --git a/BinaryFileResponse.php b/BinaryFileResponse.php index 6eddd6c28..2c4d831bb 100644 --- a/BinaryFileResponse.php +++ b/BinaryFileResponse.php @@ -189,7 +189,12 @@ public function prepare(Request $request): static } if (!$this->headers->has('Content-Type')) { - $this->headers->set('Content-Type', $this->file->getMimeType() ?: 'application/octet-stream'); + $mimeType = null; + if (!$this->tempFileObject) { + $mimeType = $this->file->getMimeType(); + } + + $this->headers->set('Content-Type', $mimeType ?: 'application/octet-stream'); } parent::prepare($request); diff --git a/Tests/BinaryFileResponseTest.php b/Tests/BinaryFileResponseTest.php index 2951cdb51..1263fa392 100644 --- a/Tests/BinaryFileResponseTest.php +++ b/Tests/BinaryFileResponseTest.php @@ -468,4 +468,15 @@ public function testSetChunkSizeTooSmall() $response->setChunkSize(0); } + + public function testCreateFromTemporaryFileWithoutMimeType() + { + $file = new \SplTempFileObject(); + $file->fwrite('foo,bar'); + + $response = new BinaryFileResponse($file); + $response->prepare(new Request()); + + $this->assertSame('application/octet-stream', $response->headers->get('Content-Type')); + } }