Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6847715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:46:44+00:00 2026-05-27T00:46:44+00:00

I have jqgrid and in that I have one custom navigation button to export

  • 0

I have jqgrid and in that I have one custom navigation button to export grid data as a excel.On onClickButton event of this button I am doing a jquery ajax call to retrieve excel data from server.My back-end is Servlet. When I am directly hitting the servlet it gives me a download dialog box but when I call this servlet by ajax call it execute the servlet code and control comes again to ajax success event properly but it not showing the download dialog box.Also after download file I want control on same page where my jqgrid is present it should not navigate to other page.
So please anyone knows why it not showing download dialog box when I am calling through ajax call.Also I have added my servlet code here please check this:

System.out.println("In side TranscationJqgrid");
            String userType = request.getParameter("userType");
            String searchField = request.getParameter("searchField");
            String searchstring = request.getParameter("searchstring");
            String searchOper = request.getParameter("searchOper");

            System.out.println("searchField = " + searchField
                    + "searchstring = " + searchstring + "searchOper = "
                    + searchOper + "userType1" + userType);
            FileOutputStream fileOutputStream = null;
            HSSFWorkbook transactionWorkbook = null;
            HSSFSheet transactionDataSheet = null;
            try {
                /**
                 * Create a new instance for HSSFWorkBook class and create a
                 * sample worksheet using HSSFSheet class to write data.
                 */
                transactionWorkbook = new HSSFWorkbook();
                transactionDataSheet = transactionWorkbook
                        .createSheet("TransactionDataSheet");
                /**
                 * Create two rows using HSSFRow class, where headerRow
                 * denotes the header and the dataRow1 denotes the cell
                 * data.
                 */
                HSSFRow headerRow = transactionDataSheet.createRow(0);
                HSSFCellStyle cellStyle = setHeaderStyle(transactionWorkbook);

                /* Read the column names from user property file */
                String webAppBaseDir = getServletContext().getRealPath("/");
                String TxPropFile = webAppBaseDir + "resource"
                        + File.separator + "transaction.properties";
                LinkedProperties tranProperties = new LinkedProperties();
                tranProperties.load(new FileInputStream(TxPropFile));
                Enumeration<Object> tranKeyEnum  =  (Enumeration<Object>) tranProperties.keys();
                /**
                 * Call the setHeaderStyle method and set the styles for the
                 * all the header cells.
                 */
                List<HSSFCell> cellList = new ArrayList<HSSFCell>();
                int idx = 0;
                while (tranKeyEnum.hasMoreElements()) {
                    String key=(String) tranKeyEnum.nextElement();
                    HSSFCell HeaderCell = headerRow.createCell(idx);
                    HeaderCell.setCellStyle(cellStyle);
                    HeaderCell.setCellValue(new HSSFRichTextString(tranProperties
                            .getProperty(key)));
                    cellList.add(HeaderCell);
                    idx++;
                }


                List<HSSFRow> rowList = new ArrayList<HSSFRow>();
                /* Get Transaction table Metadata */ 
                ColumnData columnData=txManager.getColumnData();
                String[] columnType=columnData.getColumnTypes();

                /* Get Time zone Offset of Login user. */ 
                String UTCOffset=txManager.getTimeZoneOffset(userDetail[0]);

                int row = 1;
                ResultSet txResultSet=txManager.getTranSactionRS(userDetail[0], userDetail[1]);
                while (txResultSet.next()) {

                    HSSFRow dataRow = transactionDataSheet.createRow(row);
                    for (int i = 0; i < tranProperties.size(); i++) {
                        //Suppose transaction data is retrive by Resultset instead of ResultObject.
                        if(columnType[i].equalsIgnoreCase("VARCHAR")){
                        dataRow.createCell(i).setCellValue(txResultSet.getString(i+1));
                        }else if(columnType[i].equalsIgnoreCase("INT") || columnType[i].equalsIgnoreCase("TINYINT") ){
                            dataRow.createCell(i).setCellValue(txResultSet.getInt(i+1));
                        }else if(columnType[i].equalsIgnoreCase("DECIMAL")){
                            dataRow.createCell(i).setCellValue(txResultSet.getFloat(i+1));
                        }else if(columnType[i].equalsIgnoreCase("DATETIME")){
                            dataRow.createCell(i).setCellValue(DateUtil.convertUTCToDisplayInTimezone(
                                    txResultSet.getTimestamp(i+1),UTCOffset));
                        }
                    }
                    rowList.add(dataRow);
                    row++;
                }
                fileOutputStream = new FileOutputStream("C:" + File.separator + "sampleexcel.xls");                 
                transactionWorkbook.write(fileOutputStream);
                try {
                    if (fileOutputStream != null) {
                        fileOutputStream.close();
                    }

                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                String loc="C:" + File.separator + "sampleexcel.xls";
                File                f        = new File(loc);
                int                 length   = 0;
                ServletOutputStream op       = response.getOutputStream();
                ServletContext      context  = getServletConfig().getServletContext();
                String original_filename="tran.xls";

                //
                //  Set the response and go!
                //
                //
                response.setContentType("application/octet-stream");
                response.setContentLength( (int)f.length() );
                response.setHeader( "Content-Disposition", "attachment; filename=\"" + original_filename + "\"" );

                //
                //  Stream to the requester.
                //
                byte[] bbuf = new byte[1000];
                DataInputStream in = new DataInputStream(new FileInputStream(f));

                while ((in != null) && ((length = in.read(bbuf)) != -1))
                {
                    op.write(bbuf,0,length);
                }

                in.close();
                op.flush();
                op.close();


            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                /**
                 * Close the fileOutputStream.
                 */
                try {
                    if (fileOutputStream != null) {
                        fileOutputStream.close();
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }     
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T00:46:45+00:00Added an answer on May 27, 2026 at 12:46 am

    You can’t download files by Ajax. This is a security restriction in JavaScript. JS cannot force a Save As dialogue programmatically. You need to download the file by a synchronous request instead.

    So, instead of something like

    $.get('servleturl', ...);
    

    you need to do

    window.location = 'servleturl';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this sample MVC project using jQuery grid. There is only one problem
I have a jqGrid with multiselect:true. In a click event of a button I
We have a jqGrid that shows the search box above the grid at all
I have a jqgrid that contains some data, the first column of which is
I have jqGrid 3.5 (full) mostly working. I have it retrieving data with the
I have a jqGrid on a page and users can click a button to
I have this mysql tables I want to display with jqgrid. The problem appears
Suppose I have the following (shortened for simplicity): jQuery(#grid).jqGrid({ ... ondblClickRow: function() { //
I am using ASP.NEt MVC for one of my project. In this I have
I'm using jqGrid's filterToolbar method to let users quick search/filter the grid data. I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.