Handling file serving on the backend:
@RequestMapping(value = "/fileService/temp/{temporalLink}", method = RequestMethod.GET)
public ResponseEntity<byte[]> getFileByTemporalLink(@PathVariable @NotNull String temporalLink) {
byte[] fileContent; // obtain file content
String fileName; // determine file name
String mediaType; // identify media type
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType(mediaType));
headers.setContentDispositionFormData("attachment", fileName);
return new ResponseEntity<>(fileContent, headers, HttpStatus.OK);
}
Requesting the file on the front-end:
Within the controller:
$scope.exportProductToPdf = function (someParams) {
getFileDownloadLink(someParams).then(function (response) {
$window.location.href = '/fileService/temp/' + response;
});
};
// somewhere in the code
var getFileDownloadLink = function(someParams) {
//perform promise work to find the requested file
// ultimately resolve the filename
return $q(function(resolve, reject) {
//work...
resolve("filename.png");
});
}