* @template-extends Response> */ class DownloadResponse extends Response { /** * Creates a response that prompts the user to download the file * @param string $filename the name that the downloaded file should have * @param C $contentType the mimetype that the downloaded file should have * @param S $status * @param H $headers * @since 7.0.0 */ public function __construct(string $filename, string $contentType, int $status = Http::STATUS_OK, array $headers = []) { parent::__construct($status, $headers); $sanitized = str_replace(['/', '\\'], '-', $filename); $fallback = @iconv('UTF-8', 'ASCII//TRANSLIT', $sanitized) ?: $sanitized; $fallback = preg_replace('/[^\x20-\x7e]/', '', $fallback); $fallback = str_replace('%', '', $fallback); $this->addHeader('Content-Disposition', HeaderUtils::makeDisposition(HeaderUtils::DISPOSITION_ATTACHMENT, $sanitized, $fallback)); $this->addHeader('Content-Type', $contentType); } }