0

I use varnish cache some file like *.doc *.png *.xls.

It work well while I get files from cache but *.xls.

My URI is like /attachment/show?fileId=ewer232ewe2121eeddsd. When I request a .xls file from cache, it will return a file named show whitout extension.

My server code is:

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

My question is:Why can't I get the correct file full name like attachment.getName()+".xls", and how to solve it.

PS: Is there any way to set ContentType in varnish(vcl)?

lichengwu
  • 4,277
  • 6
  • 29
  • 42

1 Answers1

1

Let me answer the last question; you can override any HTTP response header in Varnish.

Use the following VCL snippet:

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

In general you can add and remove headers at will by doing "set beresp.http.XXX = YYY;" or "unset beresp.http.XXX;" in vcl_fetch.

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

Community
  • 1
  • 1
lkarsten
  • 2,971
  • 1
  • 15
  • 11