вторник, 6 мая 2014 г.

WebSphere Application Server 7\Websphere Portal Server 7. Download files containing non-ASCII characters (Cyrillic example).

In portlet applications there is often a need to download files.
Nowadays people want to exchange files named in languages other then english.
In this post I want to describe how to download files, which contain russian characters in their names.

As you know in portlet environment one need to use ResourceResponse class to output any content to the client.

Before you output the content to the client you need to set proper http headers on response.
For file downloading you have to set header Content-disposition: attachment; filename="your file name".

HTTP specification RFC2616 prohibits the use of non-ASCII symbols in HTTP headers, but that doesn't mean you can violate this rule.

RFC2231 provides the mechanism to specify the character set before the attribute, like this filename*=utf-8''uri_encoded_filename.

If you implement this solution you might found out that sometimes Firefox allows you to save the file with the given filename and other times Firefox will allow you to save the file with blank filename.

If you look at the response in sniffer like wireshark you can find out that your header Content-disposition: attachment; filename="your file name" had been magically transferred to Content-disposition: attachment; filename=""some_junk. That's why Firefox asks you save file with a blank name.

I had find out the following workaround: prepend the "your file name" with exclamation mark, so this is the result:
Content-disposition:attachment;  filename="!" + fileName+ "\"; filename*=utf-8''" + encodedFilename

After this modification the server no longer breaks your intentions and doesn't modify your header to Content-disposition: attachment; filename=""some_junk.

This behavior I found out on WAS 7.0.0.21.

Комментариев нет:

Отправить комментарий