Vivek Mohan
Vivek Mohan

Reputation: 8336

Download file in JSP with AJAX

I have a simple JSP page, which contains 2 buttons: View and Export. When View button is clicked I will fetch data from DB, keep a copy into session and write an HTML code into a label with the data. Later when user clicks Export I want to generate an excel file in the server(with the data from session) and download it to clientside.

Excel file is successfully created at serverside. I am using an AJAX request from clientside to download Excel file from server.

JSP code:

 try{                           

                    String filepath=ExportToExcel(session.getAttribute("InvestmentDetails"));

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

                    response.setHeader("Content-Disposition","attachment; filename=\"SIPInvestment_531.xls\"");                                                                             
                    response.setStatus(200);
                    InputStream in = null;
                    ServletOutputStream outs = response.getOutputStream();                        

                    try {                            
                        File filetodownload=new File(filepath);
                        response.setContentLength(Integer.parseInt(String.valueOf(filetodownload.length())));                                
                        in = new BufferedInputStream(new FileInputStream(filetodownload));                            
                        int ch;
                        while ((ch = in.read()) != -1) {
                            outs.print((char) ch);
                        }
                    }
                    finally {
                        if (in != null) in.close(); 
                    }
                    outs.flush();
                    outs.close();                                                

                }
                catch(Exception ex){
                    str=ex.getMessage();                                          
                }

Here is the Javascript:

 xmlhttp=new XMLHttpRequest();
            xmlhttp.onreadystatechange=function(){
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                    {


                    }
            }
            xmlhttp.open("POST","/SIP/rptClientInvestmentDetails.jsp?requesttype=export",false);
            xmlhttp.send();                          

The request reaches on JSP page. And without any exception it writes to response outputstream. But no download is pop up from browser. What can be the problem?

Upvotes: 1

Views: 7315

Answers (2)

Grim
Grim

Reputation: 1974

Ajax should be used for meta-languages, not for binary files.

A simple

 <a href="/SIP/rptClientInvestmentDetails.jsp?requesttype=export"
    target="_blank">Export</a>

is all you need.

If you make sure you said the response.setHeader("Content-Disposition","attachment you should drop the target-attribute as BalusC suggested.

Upvotes: 2

Munish
Munish

Reputation: 1

I think you can use location.href="Provide the java class function name".This will transfer the control from jsp to java function without using the ajax call

Upvotes: 0

Related Questions