1

I have some reports in CSV format which I'd like to send to the browser as UTF-16LE to make Excel happy (well ... yeah).

When using ZK, I can pass Filedownload.save() a lot of information but how do I specify the encoding that should go in the response header?

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820

1 Answers1

1

There is no way to specify the encoding per download out of the box. ZK will only allow you to specify the encoding per file format by default. It works like this:

You have to create a Media instance with a format but without a ctype (content type). ZK will then look up the content type (plus the encoding) using ContentTypes.getContentType().

In this case, that would be:

String bom = "\uFEFF";
AMedia media = new AMedia(filename, "csv", null, bom + dataAsString);
Filedownload.save(media);

Note: The bom is necessary to add the Byte Order Mark to the download so Excel will do the right thing. For other file formats, you don't need this.

To set the encoding, add a file contentTypes.properties to your classpath (put it in src/main/resources/ when using Maven):

csv=text/csv;charset=UTF-16LE

ZK will parse this file during startup. When a Media object without content type but with format csv is being processed, ZK will set the content type to text/csv;charset=UTF-16LE in AMedia.setup().

The other option is to create your own implementation of the Media interface which doesn't strip the charset part from the content type.

Related:

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820