I am using REST jersey on server side and AngularJS on client side.
My requirement is to download the zip file requested by client for specific date range.
Server side code: //Time being I have hardcoded one zip file for testing
@POST
@Path("/LogRange")
@Produces({MediaType.APPLICATION_OCTET_STREAM} )
@Consumes({ MediaType.APPLICATION_JSON} )
public Response getLogsBetween(@HeaderParam(HttpHeaders.AUTHORIZATION) String authorization,
@Context HttpServletRequest request, @Context HttpServletResponse response, LogFolders folders){
StreamingOutput stream = new StreamingOutput(){
@Override
public void write(OutputStream arg0) {
// TODO Auto-generated method stub
BufferedOutputStream bus = new BufferedOutputStream(arg0);
try {
File file = new File("C:\\ProgramData\\ABC\\Logfiles\\UI.zip");
FileInputStream fizip = new FileInputStream(file);
byte[] buffer2 = IOUtils.toByteArray(fizip);
bus.write(buffer2);
bus.flush();
bus.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
return Response.status(Response.Status.OK).entity(stream).header("Content-Disposition","attachment; filename=\"log.zip\"").build();
}
Client side code:
$http({
url : urlBase + endPoint,
method: "POST",
data: formData, //this is your json data string
headers : {
'Content-Type' : "application/json",
'Authorization' : authCode,
},
responseType: 'arraybuffer'
}).success(function(response, status, headers, config) {
var blob = new Blob([response], { type: "application/zip" });
var objectUrl = URL.createObjectURL(blob);
window.open(objectUrl);
}).error(function (data, status, headers, config) {
//upload failed
});
File get downloaded in local but it is always corrupted and displays like below.
Please help me on how to download file properly.