0

我使用清漆缓存一些文件,如 *.doc *.png *.xls。

当我从缓存中获取文件但 *.xls 时,它运行良好。

我的 URI 就像/attachment/show?fileId=ewer232ewe2121eeddsd. 当我从缓存中请求一个 .xls 文件时,它将返回一个名为showwhitout 扩展名的文件。

我的服务器代码是:

if (StringUtil.null2Trim(attachment.getExtension()).equals("doc")
                    || StringUtil.null2Trim(attachment.getExtension()).equals("docx")
                    || StringUtil.null2Trim(attachment.getExtension()).equals("xlsx")
                    || StringUtil.null2Trim(attachment.getExtension()).equals("xls")) {
                response.setHeader("Content-Disposition", "attachment; filename=\""
                        + StringUtil.gbk2Iso(attachment.getName()) + "\"");
                if (StringUtil.null2Trim(attachment.getExtension()).indexOf("doc") != -1) {
                    response.setContentType("application/msword");
                }
                if (StringUtil.null2Trim(attachment.getExtension()).indexOf("xls") != -1) {
                    response.setContentType("application/vnd.ms-excel");
                }

            } else {
                if (StringUtil.null2Trim(attachment.getExtension()).equals("jpg")) {
                    response.setContentType("image/jpeg");
                } else if (StringUtil.null2Trim(attachment.getExtension()).equals("png")) {
                    response.setContentType("image/x-png");
                } else {
                    response.setContentType("image/" + attachment.getExtension());
                }
            }

我的问题是:为什么我不能得到正确的文件全名attachment.getName()+".xls",以及如何解决它。

PS:有没有办法在 varnish(vcl) 中设置 ContentType?

4

1 回答 1

1

让我回答最后一个问题;您可以覆盖 Varnish 中的任何 HTTP 响应标头。

使用以下 VCL 片段:

sub vcl_fetch {
    if (req.url ~ ".xls$") {
        set beresp.http.content-type = "application/vnd.ms-excel";
    }
}

一般来说,您可以通过“set beresp.http.XXX = YYY;”随意添加和删除标题。或“取消设置 beresp.http.XXX;” 在 vcl_fetch 中。

For your main question, I'd explore if adding a MIME envelope (header example here) helps.

于 2012-10-23T08:22:54.393 回答