views:

84

answers:

2

Hi All ,

I have written to make a zip file , now I want to trigger that servlet using Ajax and prompt the download dialog to the user , i can trigger the servlet but i dont know how to get the save dialog.

Thanks in Advance .

Vinay

+1  A: 

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.

RoToRa
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 .
Vinay
Huh? Using a normal link `<a href="/path/to/file">Download</a>` **will** "download the file on the click of the link".
RoToRa
+1  A: 

You can't use Ajax for this. You want to save the file to local disk file system, not into Javascript's memory where it can't do anything with it. Just have a plain vanilla link point to the servlet's URL and inside the servlet set the HTTP Content-Disposition header to attachment. It will pop a Save As dialogue.

Basically:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setHeader("Content-Type", "application/zip");
    response.setHeader("Content-Disposition", "attachment;filename=yourname.zip");
    // ...
}
BalusC