DownloadResponse.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCP\AppFramework\Http;
  8. use OCP\AppFramework\Http;
  9. /**
  10. * Prompts the user to download the a file
  11. * @since 7.0.0
  12. * @template S of int
  13. * @template C of string
  14. * @template H of array<string, mixed>
  15. * @template-extends Response<int, array<string, mixed>>
  16. */
  17. class DownloadResponse extends Response {
  18. /**
  19. * Creates a response that prompts the user to download the file
  20. * @param string $filename the name that the downloaded file should have
  21. * @param C $contentType the mimetype that the downloaded file should have
  22. * @param S $status
  23. * @param H $headers
  24. * @since 7.0.0
  25. */
  26. public function __construct(string $filename, string $contentType, int $status = Http::STATUS_OK, array $headers = []) {
  27. parent::__construct($status, $headers);
  28. $filename = strtr($filename, ['"' => '\\"', '\\' => '\\\\']);
  29. $this->addHeader('Content-Disposition', 'attachment; filename="' . $filename . '"');
  30. $this->addHeader('Content-Type', $contentType);
  31. }
  32. }