【问题标题】:How do I set the filename for a file download in Google AppEngine?如何在 Google AppEngine 中设置文件下载的文件名?
【发布时间】:2013-11-06 21:00:24
【问题描述】:

如何使用 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

【问题讨论】:

    标签: java google-app-engine http http-headers


    【解决方案1】:

    聚会迟到了……

    如果有帮助,setHeader 的第一个参数应该是“Content-Disposition”(尾随:应该 存在)。

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

    response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition", "attachment; filename='" + filename + "'");
    

    【讨论】:

      【解决方案2】:

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

      resp.setContentType( "text/plain" );
      resp.setHeader( "Content-Disposition:", "attachment; filename=" + fileName);
      

      【讨论】:

      • 不幸的是,这不起作用。它只是在浏览器中显示文本,由于这是一个 GWT 应用程序,它会丢失所有状态。它仍然剥离 Content-Disposition 标头。想要强制下载。
      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2011-07-28
        • 2012-01-01
        • 1970-01-01
        • 2012-08-22
        • 2016-08-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多