I have rest
in java
where I get a byte[]
of a ZIP file. If I access the API URL through the browser, the file is usually downloaded without error.
But if I try to download the file through an implementation using new Blob([response.data], {type: "application/zip"});
on the front end, the file is downloaded with error.
Could anyone tell me where the problem is?
API
@GET
@Path("/ucmFile")
@Produces("application/zip")
public Response buscarUCMFile(@QueryParam("docName") String docName) throws Exception {
byte[] file = null;
String fileName = null;
try {
NDC.push(" [buscarUCMFile - userAPI: " + userAPI.getUserAPI() + "] ");
// Busco o arquivo e fileName
} catch (ValidationException e) {
logger.error(e.getMessage(), e);
Util.validationApplicationException(e.getMessage());
} catch (Exception e) {
logger.error(e.getMessage(), e);
Util.applicationException(e);
} finally {
NDC.pop();
}
return Response.ok(file).header("Content-Disposition", "attachment; filename=" + fileName)
.header("Content-Length", file.length).build();
}
AngularJS
$http.get(config.HOST_API + vm.listService.config.baseURL + "/ucmFile?docName="+docName).then(function(response) {
if(response.status === 200) {
var a = window.document.createElement('a');
var blob = new Blob([response.data], {type: "application/zip"});
var url = window.URL.createObjectURL(blob);
// Append attributes
a.href = url;
a.download = fileName;
// Append anchor to body.
document.body.appendChild(a);
a.click();
// Remove anchor from body
document.body.removeChild(a);
} else {
CustomAlertError("Não foi possivel realizar o download do arquivo.");
}
}, function(response) {
CustomAlertError("Não foi possivel realizar o download do arquivo.");
});