Thursday, August 28, 2008

content disposition - downloading files from browser

Recently I had to deal with opening a csv file from a browser.
After some research I found that the "content-disposition" header of the HttpServletResponse provides a lot of useful things.

A general rule of thumb to set the value of "content-disposition":
* To open the file in a browser, set it to "inline; filename=MyFile.pdf"
* To download the file, set it to "attachment; filename=MyFile.pdf"

Displaying images I always set the content disposition to the "inline" option.
The following code snippet sets the content type to csv and the content disposition of the file that is being opened in the browser to a ".csv" extension.

HttpServletResponse response = <initialize it here>
response.setContentType("text/csv");
response.addHeader("content-disposition", "attachment; filename=" + "MyFile.csv");


For more information on the HttpServletResponse class look at the API Docs.

Also, I found this cool link where it explains on
how to send a pdf file.

Some forums I came across that deal with the same subject.
Servlet file download works in firefox but not in IE
Opening MS word document in browser

Monday, February 11, 2008

Setting the character encoding

Setting the character encoding is very important to import style sheets or Java scripts in an XML file. Normally this is not forgotten in a HTML or a JSP file.
Character encoding is set to unicode in the following line.
<meta equiv="Content-Type" content="text/html; CHARSET=UTF-8">

Once you have the above line in your xml/html file, you can link to style sheets successfully.
<link title="theme" rel="stylesheet" href="styles/tabstyle.css" type="text/css"/>

The XML file looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<html>
<head>
<title> Test </title>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8"/>
<link title="theme" rel="stylesheet" href="styles/tabstyle.css" type="text/css"/>

You can learn more about unicode in the following link.
http://www.w3.org/International/O-charset

Thursday, February 7, 2008

How to put a nbsp in xml without getting error

When you put a & nbsp ; in your xml file and then display it via an XSLT, you will get the following error:
"Fatal Error: The entity nbsp is referenced but not declared"

A work-around for this is to substitute "& nbsp ; " with "& #160 ; " in the xml file.
The error will not occur.


Resource

Setting class attribute to change style of an element

You can change the class (style) of an attribute in JavaScript.
An easy way to do this is:

element.setAttribute("class","styleClassName");
This works in firefox but not in IE

Another way to do it is:
element.setAttrbute("className", "styleClassName");
This works in IE but not in firefox.

An easy work-around that I found on web and which works on both IE and Firefox is:
element.setAttribute((document.all ? 'className' : 'class'), "styleClassName");

Resources:
http://www.quirksmode.org/bugreports/archives/2005/03/setAttribute_does_not_work_in_IE_when_used_with_th.html
http://www.digitalmediaminute.com/article/1394/the-browser-dom-and-the-class-attribute