0

I am trying to download a file using ajax and a servlet, but the maximum I get is that in succes I get the file parseado

I have this section of the servlet:

else if(type.equals("downloadDocument")){
            String file = request.getParameter("filePath");
            File f = new File(file);

            if (f.exists() && f.isFile()){
             OutputStream out = response.getOutputStream();
             FileInputStream in = new FileInputStream(f);
             byte[] buffer = new byte[4096];
             int length;
             while ((length = in.read(buffer)) > -1){
                 out.write(buffer, 0, length);
             }
             in.close();
             out.flush();
            }

And that call:

    $.ajax({
        type : "POST",
        url : "./ServletDocuWindow?downloadDocument",
        data : datos,
        success : function(r) {

        }
    });
Oshant
  • 159
  • 1
  • 17
  • Its not possible via AJAX. [This](http://stackoverflow.com/questions/4545311/download-a-file-by-jquery-ajax) will probably answer you question! – ShahzadIftikhar Apr 13 '17 at 10:09
  • @ShahzadIftikhar It is. It's not easy nor supported by all browser, but with the filesystem API and other similar ones you can do it. For example, [mega.co.nz does it](http://stackoverflow.com/questions/15994554/download-files-like-mega-co-nz) – BackSlash Apr 13 '17 at 10:13
  • And without using ajax? What I need at the end is to download a file I get from the server – Oshant Apr 13 '17 at 10:19
  • so if I understand correctly,there is some link and on user click,you do not want to refresh page but start download right away.Is this how ur looking to do it? – Shivam Aggarwal Apr 13 '17 at 10:23
  • approximately. I have a list with a list of files. The user selects the one he wants and presses the "Download" button, so he should start the download without reloading the web. Thank you all! – Oshant Apr 13 '17 at 10:26
  • 1
    The easiest way to download a file is to provide a direct lik to the file URL. You may have to add the file extension in your web.xml so that the servlet doesn't call your doPost method but immediately proceeds with the download. – dsp_user Apr 13 '17 at 10:59

2 Answers2

1

Instead of downloading the file from AJAX, I pass the request on new separate window. Then, my servlet is called and file is getting downloaded on my local. Also, new window is closed when file starting downloaded

I guess you need to add following code and you should be good to go.

    response.setContentType("application/octet-stream");
    response.setContentLength((int) downloadFile.length());

    // set headers for the response
    String headerKey = "Content-Disposition";
    String headerValue = String.format("attachment; filename=\"%s\"", AppUtility.getConvertedString(fileName)); //to ensure that there are no space in the file name
    response.setHeader(headerKey, headerValue);
Ankit
  • 2,126
  • 4
  • 33
  • 53
0

Following the advice of @dsp_user I used a to call the servlet and it works perfectly

var doc = document.getElementById("windowDocumentId").value;
        var index = $("#lvDocuments").data("kendoListView").select().index();


        var link = document.createElement("a");
        link.download = $("#lvDocuments").data("kendoListView").dataSource.view()[index].itemText;
        link.href = "./ServletDocuWindow?" + doc + "," +$("#lvDocuments").data("kendoListView").dataSource.view()[index].itemText;
        link.click();
Oshant
  • 159
  • 1
  • 17