This the best solution that i found, Thanks to Link1 and Link2
file.html
<a ui-sref="state" ng-click="downloadDoc(doc)" ></a>
Controller.js
$scope.downloadDoc = function(doc){
var a = document.createElement("a");
document.body.appendChild(a);
var fileExt = doc.nomeFile.substring(doc.nomeFile.lastIndexOf('.')+1, doc.nomeFile.length) || doc.nomeFile;
var fileType = AllegatiService.GenerateFileType(fileExt);
AllegatiService.getFile(doc).success(function(data){
var file = new Blob([data], {type:fileType});
var fileURL = window.URL.createObjectURL(file);
a.href = fileURL;
a.download = doc.nomeFile;
a.click();
});
}
service.js
var getFile = function(doc){
return $http({
method: 'POST', url:'download/getFile',responseType: 'arraybuffer',
data: doc
});
}
controller java
@RequestMapping(value = "/getFile", method = RequestMethod.POST)
public ResponseEntity<byte[]> getPdf(@RequestBody DocDto docDto ) throws IOException{
String fileType = getFileType(docDto.getNameFile());
File file = docDto.getFile();
String filename =docDto.getNameFile();
FileInputStream fileStream;
try {
fileStream = new FileInputStream(file);
byte[] contents = IOUtils.toByteArray(fileStream);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType(fileType));
headers.setContentDispositionFormData(filename, filename);
ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(contents, headers, HttpStatus.OK);
return response;
} catch (FileNotFoundException e) {
System.err.println(e);
} catch (IOException e) {
System.err.println(e);
}
return null;
}
public String getFileType(String nameFile) {
String fileExt = nameFile.substring(nameFile.lastIndexOf('.')+1, nameFile.length());
String FileType ="";
switch (fileExt.toLowerCase()) {
case "doc":
case "docx":
FileType = "application/msword";
break;
case "xls":
case "xlsx":
FileType = "application/vnd.ms-excel";
break;
case "pps":
case "ppt":
FileType = "application/vnd.ms-powerpoint";
break;
case "txt":
FileType = "text/plain";
break;
case "rtf":
FileType = "application/rtf";
break;
case "pdf":
FileType = "application/pdf";
break;
case "msg":
case "eml":
FileType = "application/vnd.ms-outlook";
break;
case "gif":
case "bmp":
case "png":
case "jpg":
FileType = "image/JPEG";
break;
case "dwg":
FileType = "application/acad";
break;
case "zip":
FileType = "application/x-zip-compressed";
break;
case "rar":
FileType = "application/x-rar-compressed";
break;
}
return FileType;
}