Я пытаюсь создать функциональность загрузки изображений. У меня есть следующий код в
контроллер angularJs
$scope.download= function(){
smoothingFilterService.downloadImage($scope.imageObj.destinationPath).then( function (data, status, headers, config) {
var anchor = angular.element('<a/>');
anchor.attr({
href: 'data:application/jpg,' + data.data,
target: '_self',
download: data.headers.filename
});
angular.element(document.body).append(anchor);
anchor[0].click();
},function (error) {
})
}
Код в угловую Сервис
downloadImage:function(filePath){
var req={
method:'GET',
url:'/iprocessor/downloadImage',
params: {filePath: filePath},
transformResponse: function (data, headers) {
var response = {};
response.data = data;
// take note of headers() call response.headers = headers();
return response;
}
}
return $http(req)
}
Код HTML
<a class=" small" href="" ng-click="download()">Download </a>
Код внутреннего контроллера Java
@RequestMapping(value="/downloadImage",method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
public ResponseEntity<byte[]> downloadImage(@RequestParam String filePath, HttpServletResponse response) throws IOException {
response.setHeader("Content-disposition","attachment; filename=sharp.jpg");
File file= new File(filePath);
FileSystemResource fileResource = new FileSystemResource(file);
byte[] base64Bytes = Base64.encodeBase64(IOUtils.toByteArray(fileResource.getInputStream()));
HttpHeaders headers = new HttpHeaders();
headers.add("filename", fileResource.getFilename());
return ResponseEntity.ok().headers(headers).body(base64Bytes);
}
Когда я нажимаю на ссылку для скачивания, он загружает файл с именем «скачать» без какого-либо расширения. Я ожидал файл «sharp.jpg».
Http Запрос вызова

