Problem while downloading a tab delimited file to excel using Servlet

N

Niraj Agarwal

Hi,

I am trying to download a tab-delimited file to client machine using
servlet.
I am using IE5.5 browser to test the application.
Here is the code in my servlet, which is sending the file to the
client machine.

ServletOutputStream out = response.getOutputStream();

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "inline;
filename=\""+getFileNameOnly()+".xls\"");

// Generate tab delimited string buffer
StringBuffer sbRet = generateXlsFormat();

// Send it to client
out.write(sbRet.toString().getBytes());

out.close();

I want to download (open) this file directly to a new browser window,
or in excel with the filename i am passing from the servlet.

The problem is that, it opens the file in excel but the file name is
the url that i posted to get this file, it is not that i set in
servlet.

I tried all permutations and combinations but nothing is working.

Can someone please help me out to resolve this problem?

Thanks in advance,
Niraj
 
G

Gregory A. Swarthout

Hi,

I am trying to download a tab-delimited file to client machine using
servlet.
I am using IE5.5 browser to test the application.
Here is the code in my servlet, which is sending the file to the
client machine.

ServletOutputStream out = response.getOutputStream();

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "inline;
filename=\""+getFileNameOnly()+".xls\"");

// Generate tab delimited string buffer
StringBuffer sbRet = generateXlsFormat();

// Send it to client
out.write(sbRet.toString().getBytes());

out.close();

I want to download (open) this file directly to a new browser window,
or in excel with the filename i am passing from the servlet.

The problem is that, it opens the file in excel but the file name is
the url that i posted to get this file, it is not that i set in
servlet.

I tried all permutations and combinations but nothing is working.

Can someone please help me out to resolve this problem?

Thanks in advance,
Niraj


We do virtually the same thing and it works. The only differences I
see
are:

1) We use "application/x-ms-excel" for the content-type.
2) We set the headers "Cache-Control" and "Pragma" to "".
3) Our "Content-disposition" header has the lower-case "d".
4) We set the content length a la:
response.setContentLength(sbRet.toString().getBytes().length);
5) We flush the output after writing it a la:
response.getOutputStream().flush();

See if one or more of the above fixes your problem.

Greg
 
N

Niraj Agarwal

Thanks Greg,

I tried all things you mentioned, but it is still the same. Can you
share your code with me if possible. Also can it be the problem with
AppServer. I am using WebLogic 8.1.

Also when i use "Content-disposition-type" as "attachment" instead of
"inline", it shown "Save As" dialog, but if i use "open from current
location" option it ask for 3 times before opening the file in excel,
but this time it uses the right file name.

Thanks,
Niraj
 
N

Niraj Agarwal

Thanks Greg,

I tried all the things you mentioned but still the result is the same,
file opened in excel does not have right file name. Can you share your
code that is working fine, if you can. Can it be problem with
Application server, we are using Weblogic 8.1.

Niraj
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top