I want to download a text file (ex:-somthing.txt). When I use an ancor tag , the file is downloaded, but i want to download the file using an ajax call.
HTML code:
<body>
<a href="#" id="exportViewRule">Export</a>
</body>
JavaScript code :
$("#exportViewRule").click(function(){
$.ajax({
url : "/download/myDir/exportFile",
type : "GET",
contentType : "text/plain",
success : function(data){
alert(data);
}
});
});
java code:
@Path("/myDir")
public class IdnsDataHandler {
@GET
@Path("/exportFile")
@Produces("text/plain")
public Response exportFile(){
File file=new File("/home/cerdik/Desktop/some.text");
ResponseBuilder response=Response.ok((Object)file);
response.header("Content-Disposition","attachment; filename=export-file.text");
return response.build();
}
}
When i use this code bellow (without javascript), the download works.
HTML code:
<body>
<a href="./download/myDir/exportFile" id="exportViewRule">Export</a>
</body>