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
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
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
.
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");
// ...
}