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();
}}
}

