Response::withFile() and Response::withFileDownload()#91
Conversation
|
In Firefox 67.0 (64-Bit) the file name in the "Save as" dialog would be wrong if we use If instead we write $disposition .= '; filename*=UTF-8\'\'' . urlencode($fileName);then all the browsers mentioned above display the correct file name. See http://test.greenbytes.de/tech/tc2231/#encoding-2231-char for more information. Note that this works only, if the |
|
If we pass a public function __invoke(ServerRequest $request, Response $response, array $args = []): Response
$stream = $psr17Factory->createStreamFromFile('data.json');
return $response->withFileDownload($stream);
}Chrome and Opera defaults to the filename Probably we should raise an exception if a user tries to do that? Or is that the responsibility of a user? |
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition |
Of course, if the url contains a path from which the user agent can parse the filename, then this is okay. But obviously, if the download would be triggered by calling By the way, the same holds for any paths ending with a slash (of course that should be avoided). So if we define a route $app->get('/hello/', function (\Slim\Http\ServerRequest $request, \Slim\Http\Response $response, array $args) {
$psr17Factory = new Psr17Factory();
$stream = $psr17Factory->createStream('1234');
return $response->withFileDownload($stream);
});then a request to So I guess this is a problem that the user agents should solve for themselves. It is not our duty to prevent that, right? |
I think this is not our problem. But we can also set default filename to "Download" (don't throw error) |
Let's not throw an error but append a default filename as suggested like "download" or "attachment" when a |
Would it make sense to try to get the file name from the metadata |
Yea for sure why not. |
Resuming to adopt the proposed direction in #88
New Response Methods:
Behavior
The
$fileparameter can be astringwhich points to a file, an existingresourcehandle or aStreamInterfaceas proposed by @roxblnfkThe
$nameparameter overrides the defaultattachmentvalue for theContent-Dispositionheader. The given$nameis filtered throughurlencode()to ensure it is a valid header value.The
$contentTypeparameter accepts astringto override theContent-Typeheader to a user defined value. Defaults totruewhich attempts to detect the mime type viamime_content_type()and falls back toapplication/octet-streamotherwise. If set tofalsetheContent-Typeheader is not appended at all.Closes #82