Sunday, June 17, 2007

Images in MHT not visible: Issue 1

I've witnessed a couple of of problems related to the images in MHT (web-archive) files streamed from a J2EE web-application. If you are facing similar problems then read on to know why it happens and how can you fix them. Even though some of the solutions are discussed and illustrated Java, the same applies to other web technologies trying to serve MHT files. I will be considering only the most common browsers, Mozilla Firefox and MS Internet Explorer as I've not tried the fixes with other browsers.

I will post various problems and their solutions over the next few days.

MHT Issue 1:
The MHT file shows and open save dialog box.
OR
MHT file is rendered as and ASCII text file.
Make sure that your browser supports MHT. By default Microsoft Internet Explorer supports MHT files. If you are using Mozilla Firefox then you can install a plugin to render MHT files. As of now it isn't available for the latest version of Firefox. So you might have to consider switching an older version. But if you don't have the plugin installed then you will always get the Open/Save dialog in Firefox.

The problem could also be with the response sent by the server. The HTTP headers of the response sent from server should be set correctly. The content type should be set to "multipart/related". Content-disposition to "filename=yourfilename.mht". You should also add "charset=UTF-8" if your file contains any internationalized UTF-8 characrters. So set the complete Content-disposition to "filename=yourfilename.mht;charset=UTF-8". Make sure that the url used to access the file contains "yourfilename.mht" (or "xyz.mht" or "abc.mht"). It basically helps the browser understand that you are accessing a MHT file.
Sample URLS:
Direct URL
"http://localhost:8080/context/myfile.mht"
OR
In case of URL to a a servlet that streams the file you may add a dummy requst parameter like:
"http://localhost:8080/context/streamServlet?&dummyParm=foo.mht"

Note that the foo.mht is just a dummy parameter to help browser understand that you are accessing a MHT file. It need not be the actual name of file.

Once all of the above are done, the browser should be able identify that the file is of type MHT and it will then try to render it accordingly.

No comments: