2

I have a file to dowmnload from servlet and i need to download it when user clicks download button without being page refreshed.Hiw can i achieve this. Here is my jsp code.I m allowing user to dpownload the file by inputting the credentials in search box.So my file searching logic is in servlet.

<form action="Download_Servlet" class="download" method="post">     
Search:<input type="text" name="dropdown" id="datedropdown">
<input type="submit" id="downloadRecords" value="Download">

Here is my servlet code

response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition","attachment;filename=abc.csv");
ServletOutputStream out = res.getOutputStream();
for (Order traverse : orderMap.values()) 
    {   
        out.write(traverse.toString().getBytes());
        out.write("\n".getBytes());
        out.flush();
    }

Reason for using for loop is to write hundreds of records into file and then flush it..Nbow the concern is when user clicks download button page is trying to be redirect but there is no code in servlet for redirecting the response so why this is happening..I want is when download button clicked, file only should be downloaded without page being redirected.

  • Okay, and where is your ajax code? – drgPP Aug 24 '16 at 07:35
  • I have not used ajax for this but when i searched on net people saying ajax is the best one for this solution but cant get the exact solution..So asking you people how can i achieve this. – Rohit Kolhey Aug 24 '16 at 07:39
  • 2
    Page refresh/redirect is **not** the default behavior when the servlet returns a `Content-Disposition:attachment` response. It should work outright. Your problem is caused elsewhere. The cause is unfortunately not visible in the information provided so far. To naildown the root cause, create a [mcve]. There's at least absolutely no need for an overcomplicated ajax based solution. – BalusC Aug 24 '16 at 10:16
  • Thanks guys for the reply..I want to tell you that i got the solution for that...as on button clicking,it was not the redirection of the page, as it was the jpg file showing "Loading" was being called every time i hit the button..So i hide that file whenever form submitted.It is done by using .hide() on the id of the submit button of the form. – Rohit Kolhey Aug 24 '16 at 11:46
  • See also [Submit form without page reloading](https://stackoverflow.com/questions/2866063/submit-form-without-page-reloading) – Vadzim Nov 12 '19 at 20:14
  • @BalusC, is there any way to refresh the JSP page after the csv content is downloaded in the browser. I have a scenario where i want to refresh the table in jsp such that downloaded record should be erased from the show page, right now manually i have to refresh once csv is downloaded – rinilnath Feb 04 '22 at 11:28

1 Answers1

-1

Here's a function :

function ajax_download(url, data) {
    var $iframe,
        iframe_doc,
        iframe_html, 
        input_name;

    if (($iframe = $('#download_iframe')).length === 0) {
        $iframe = $("<iframe id='download_iframe'" +
                " style='display: none' src='about:blank'></iframe>"
               ).appendTo("body");
    }

    iframe_doc = $iframe[0].contentWindow || $iframe[0].contentDocument;

    if (iframe_doc.document) {
        iframe_doc = iframe_doc.document;
    }

    iframe_html = "<html><head></head><body><form method='POST' action='" +
                  url +"'>" +
                  "<input type=hidden name='" + input_name + "' value='" +
                  JSON.stringify(data) +"'/></form>" +
                  "</body></html>";

    iframe_doc.open();
    iframe_doc.write(iframe_html);
    $(iframe_doc).find('form').submit();
}

Usage :

$(document).on('click', '#download_button_id', function(){
    ajax_download('http://www.mridulahuja.com/uploads/1/3/8/6/13860206/handy_notes_v2.0.rar');
});
mrid
  • 5,782
  • 5
  • 28
  • 71
  • can i send the value of the search box to servlet using ajax call then in return i will just alert that file Downloaded..what do you think? But in return ,to be able to run the success function we need to send the something in response from servlet to ajax right or wrong? if right then what will be the syntax for success function to get the nothing from servlet.... – Rohit Kolhey Aug 24 '16 at 09:52