DBからファイルをダウンロードする方法を作成しました。フロントエンド側の全体の方法は、ステータス200を返しますが、全体のファイルが見えます:HTTPステータスが200であるにもかかわらずAngularはファイルイベントをダウンロードしません
Java
コントローラ:
@PreAuthorize("hasAnyAuthority('CLIENT', 'WORKER')")
@RequestMapping(value = "", method = RequestMethod.GET, produces = MediaType.ALL_VALUE)
public ResponseEntity<byte[]> downloadFile(@RequestParam("id") Long id, HttpServletRequest httpRequest, HttpServletResponse response) throws FileNotFoundException, IOException{
File file = fileService.downloadFile(id); //custom File class
if (file == null) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
}
byte[] base64Bytes = Base64.encodeBase64(file.getData());
HttpHeaders header = new HttpHeaders();
header.setContentType(new MediaType("application", "pdf"));
header.set("Content-Disposition", "inline; filename=" + file.getName());
header.setContentLength(file.getData().length);
return ResponseEntity.ok().headers(header).body(base64Bytes);
}
JS Controller
ファイル:
$scope.downloadFile = function (id) {
FileService.get({id: id}).$promise.then(function(result) {
console.log('download file');
});
}
HTML
ファイル:
<li>
<button ng-click="downloadFile(f.id)">{{f.name}}</button>
</li>
この[リンク]をご覧ください(http://stackoverflow.com/questions/43715032/how-to-save-following-content-as-pdf-in-angularjs-using-filesaver-js/43715244# 43715244) –
アップロードしたくありません。このケースは、Webブラウザのdbからファイルをダウンロードすることです。 – bielas