Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.5] Add stream download response #22764

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion src/Illuminate/Routing/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,33 @@ public function stream($callback, $status = 200, array $headers = [])
return new StreamedResponse($callback, $status, $headers);
}

/**
* Return a new streamed response as a file download from the application.
*
* @param \Closure $callback
* @param string $name
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string|null according to your method signature

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @mfn

Thanks for the PR review. I have pretty much copied the doc block from the "download" method. Would you also like me to make the same change to that doc block as well?

* @param int $status
* @param array $headers
* @param string|null $disposition
* @return \Symfony\Component\HttpFoundation\StreamedResponse
*/
public function streamDownload($callback, $name = null, $status = 200, array $headers = [], $disposition = 'attachment')
{
$response = new StreamedResponse($callback, $status, $headers);

if (! is_null($name)) {
$disposition = $response->headers->makeDisposition(
$disposition,
$name,
$this->fallbackName($name)
);

$response->headers->set('Content-Disposition', $disposition);
}

return $response;
}

/**
* Create a new file download response.
*
Expand All @@ -125,12 +152,23 @@ public function download($file, $name = null, array $headers = [], $disposition
$response = new BinaryFileResponse($file, 200, $headers, true, $disposition);

if (! is_null($name)) {
return $response->setContentDisposition($disposition, $name, str_replace('%', '', Str::ascii($name)));
return $response->setContentDisposition($disposition, $name, $this->fallbackName($name));
}

return $response;
}

/**
* Returns a string containing only ASCII characters that is semantically equivalent to $name.
*
* @param string $name
* @return string
*/
protected function fallbackName($name)
{
return str_replace('%', '', Str::ascii($name));
}

/**
* Return the raw contents of a binary file.
*
Expand Down