file download problem

I am doing file uploading and downloading using myfaces.uploading works fine...downloading also works fine except for .txt and .html files...When I try to open a .txt file(say file1.txt) what I get is contents of file1.txt+some garbage values(This garbage values are nothing but the source code of the jsp page in which I have the download button).Is this a browser problem?what is the solution?Pls have a look at my code..

public void downloadFile(ActionEvent e) {

// i am getting the file to be displayed from db table attachment_table

if(attachment_table.getAttachment_name()!=null)

{

byte[] jj= attachment_table.getAttachment();

System.out.println( jj.length );

String kk= attachment_table.getAttachment_name();

String fileExtension=kk.substring(kk.lastIndexOf(".")+1);

try{

//if (!FacesContext.getCurrentInstance().getResponseComplete() )

//{

FacesContext faces = FacesContext.getCurrentInstance();

HttpServletResponse response = (HttpServletResponse) faces.getExternalContext().getResponse();

// response.setContentType("application/x-download");

// if (fileExtension .equals("txt") )

// response.setContentType("text/plain");

response.setContentLength(jj.length);

//response.setHeader( "Content-disposition", "inline; filename="+ kk);

response.setHeader("Content-Disposition","attachment; filename=\""+kk+"\"");

// response.setHeader("cache-control", "no-cache");

//response.setHeader("cache-control", "must-revalidate");

ServletOutputStream out;

out = response.getOutputStream();

out.write(jj);

// in.close();

out.flush();

out.close();

//StateManager stateManager = (StateManager)

//faces.getApplication().getStateManager();

// stateManager.saveSerializedView(faces);

faces.getResponseComplete();

//}

}

catch(Exception ex)

{

ex.printStackTrace();

}}

}

[2003 byte] By [Mil_Gripa] at [2007-11-27 6:18:02]
# 1
Please use [ code ] tags to nest your code in, it will increase the readability.In the meanwhile you can take a look to the downloadFile() snippets provided here http://balusc.xs4all.nl/srv/dev-jep-pdf.html Those should work flawlessly for *any* file type.
BalusCa at 2007-7-12 17:31:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Hi BaluC,

thanks for your reply...i have tried all the options in your downloadFile function .But still the problem is there.why this problem comes only with txt/html?...i am able to open all the other file types like .doc,.xls,.zip,.xml etc..........

The problem may be the below mentioned one

.xls is opening in MS excel

.doc is opening in Ms word like that...but a txt/html will open in the browser itself...Is that the reason for the garbage values which I am getting with the files opening?

Mil_Gripa at 2007-7-12 17:31:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
> but a txt/html will open in the browser itself...Are you sure that the content disposition is set to 'attachment'?
BalusCa at 2007-7-12 17:31:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
ya..that is sure..that is why ,when I click on the file name i am getting the File Download Dialogue box ....i am using IE 6 with SP2...
Mil_Gripa at 2007-7-12 17:31:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

a .txt or .html file will open in the browser itself(as they don't have any other application to open the file like .excel or .word, which you have already mentioned).Moreover i think this issue has nothing to do with JSf or myfaces.This is a browser issue only... In your code try to add

if (fileExtension .equals("txt") )

response.setContentType("text/plain");

not sure it will work or not?

jack_drewa at 2007-7-12 17:31:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

hi jack thanks for the reply...

i tried your suggestion but failed.

Please see the below mentioned line in my code

>>response.setHeader("Content-Disposition","attachment; >>filename=\""+kk+"\"");

Here kk is the file name with extension(like file1.xls)

So no need to give

>>if (fileExtension .equals("txt") )

>>response.setContentType("text/plain");

because by looking at the extension of the file, IE will automatically decide the tool in which the should be opened.

Any more help?

Mil_Gripa at 2007-7-12 17:31:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

> http://balusc.xs4all.nl/srv/dev-jep-pdf.html

I'm using facelets, so there's no way to use<%

mypackage.MyBean myBean = (mypackage.MyBean) session.getAttribute("myBean");

myBean.downloadPdfInline();

%>

Could you please give some more help in the facelets situation?

Thank you.

Thaia at 2007-7-12 17:31:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
Decide kind of a FileServlet which only accepts encrypted ID's as parameter. Then call this servlet in a h:outputLink with target="_blank". http://balusc.xs4all.nl/srv/dev-jep-img.html might provide some insights.
BalusCa at 2007-7-12 17:31:40 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...