当客户端请求文件时,我使用此代码发送它:
public static Result download(String file) {
File file = getRealFile(file);
return Ok(file);
}
但我发现浏览器不会下载它,而是显示它的内容。响应头:
Content-Type text/plain
Transfer-Encoding chunked
发送文件的正确方法是什么?
更新
根据 Razvi 的回答,我发现这个问题的答案似乎很好:https ://stackoverflow.com/a/1074925/342235
但是我们真的需要设置这么多的标题吗?
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$filepath");
header("Content-Type: mime/type");
header("Content-Transfer-Encoding: binary");
// UPDATE: Add the below line to show file size during download.
header('Content-Length: ' . filesize($filepath));