2

如何使用 Google AppEngine 设置文件下载的文件名?我尝试设置Content-Disposition,但 AppEngine 在生产中删除了该标题(在开发中工作)。

代码

    resp.setContentType( "application/octet-stream" );
    resp.setHeader( "Content-Disposition:", "attachment; filename=" + "\"" + fileName + "\"" );

开发标题

Content-Type: application/octet-stream
Content-Disposition: attachment; filename="logs-2013-11-06-15-23-21.csv"

生产标头

HTTP/1.1 200 OK
Content-Type: application/octet-stream
X-AppEngine-Estimated-CPM-US-Dollars: $0.001434
X-AppEngine-Resource-Usage: ms=220 cpu_ms=224
Date: Wed, 06 Nov 2013 20:25:00 GMT
Pragma: no-cache
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Cache-Control: no-cache, must-revalidate
Server: Google Frontend
Content-Length: 12713
Alternate-Protocol: 80:quic

参考

https://developers.google.com/appengine/docs/java/#Java_Responses https://code.google.com/p/googleappengine/issues/detail?id=10247

4

3 回答 3

3

派对迟到了……

只要有帮助,setHeader 的第一个参数应该是“Content-Disposition”(Trailing :应该出现)。

然后,chrome 浏览器的保存对话框会识别提供的文件名值。(在我的应用程序中,内容类型被设置为“application/octet-stream”)。这对我有用:

response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename='" + filename + "'");
于 2014-05-18T14:33:28.643 回答
1

由于您要下载的文件是 csv 文件,请尝试

resp.setContentType( "text/plain" );
resp.setHeader( "Content-Disposition:", "attachment; filename=" + fileName);
于 2013-11-07T21:27:30.970 回答
0

尝试“text/csv”或“application/csv”而不是“text/plain”,两者都有效。

于 2013-12-17T18:53:44.543 回答