Download a file from Servlet using Ajax
Asked Answered
P

3

15

I have created a zip file in my servlet. Now I would like to trigger that servlet using Ajax and prompt the download dialog to the user. I can trigger the servlet, but I don't know how to get the save dialog. How can I achieve this?

Protamine answered 17/8, 2010 at 12:17 Comment(0)
N
10

You can't "download a file using AJAX". AJAX is about downloading data from a server for JavaScript to process.

To let the user download the file either use a simple link to the file/servlet, or if you really, really need to use JavaScript, then assign the URL to document.location.href.

Also you need to make sure that the server (or in this case the servlet) sends the appropriate MIME type, in case of a ZIP file most likely application/zip.

Nyctalopia answered 17/8, 2010 at 12:28 Comment(2)
Problem is On a link i have to download a file , i dont want to navigate to another page , I have tried out by calling a servlet by forms , that works fine but now I need to download the file on the click of the link .Protamine
Huh? Using a normal link <a href="/path/to/file">Download</a> will "download the file on the click of the link".Nyctalopia
S
9

You can't use Ajax for this. You basically want to let the enduser save the file content to the local disk file system, not to assign the file content to a JavaScript variable where it can't do anything with it. JavaScript has for obvious security reasons no facilities to programmatically trigger the Save As dialog whereby the file content is provided from an arbitrary JavaScript variable.

Just have a plain vanilla link point to the servlet URL and let the servlet set the HTTP Content-Disposition header to attachment. It's specifically this header which will force the browser to pop a Save As dialog. The underlying page will stay same and not get refreshed or so, achieving the same experience as with Ajax.

Basically:

<a href="fileservlet/somefilename.zip">download file</a>
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // ...

    response.setHeader("Content-Type", getServletContext().getMimeType(fileName));
    response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");

    // ...
}

That could also be done in JavaScript as below without firing a whole Ajax call:

window.location = "fileservlet/somefilename.zip";

Alternatively, if you're actually using POST for this, then use a (hidden) synchronous POST form referring the servlet's URL and let JavaScript perform a form.submit() on it.

See also:

Schuller answered 17/8, 2010 at 13:14 Comment(0)
E
0
function down() {

    var url = "/Jad";
    var xmlhttp;

    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function() {
        //alert("xmlhttp.status" + xmlhttp.status);
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

        }

    }


    xmlhttp.open("GET", url, true);
    xmlhttp.send();


    var elemIF = document.createElement("iframe");
    elemIF.src = url;
    elemIF.style.display = "none";
    document.body.appendChild(elemIF);
}
Englis answered 15/3, 2012 at 12:24 Comment(1)
The ajax part is completely unnecessary. It won't work anyway (as answered by others). Just the last four lines are sufficient. Much eaiser is to use window.location = url; if you can guarantee that the server side returns Content-Disposition:attachment.Schuller

© 2022 - 2024 — McMap. All rights reserved.