gpt4 book ai didi

download - 如何将文件发送到浏览器进行下载?

转载 作者:行者123 更新时间:2023-12-04 11:41:32 25 4
gpt4 key购买 nike

当客户端请求文件时,我使用此代码发送它:

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));

最佳答案

您需要设置 Content-Disposition header 。我相信标题的值应该是 attachment .见 Manipulating the response doc 为此。

对于 play 2.0,以下工作无需明确设置标题:
ok(new FileInputStream(file))

关于download - 如何将文件发送到浏览器进行下载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11559304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com