0
ファイルasyncをダウンロードしたいと思います。AJAXとSpring Bootでファイルをダウンロードする
<a th:href="@{'download/' + ${person.number}}">download</a>
と::私はURLから呼び出されたときに実行される作業の同期方法持って
@RequestMapping(value = "/download/{number}", method = RequestMethod.GET)
public ResponseEntity<byte[]> downloadFile(@PathVariable Long number) throws Exception {
byte[] array = Files.readAllBytes(file.toPath());
String fileName = "attachment; filename="+number+".xls";
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("charset", "utf-8");
responseHeaders.setContentType(MediaType.valueOf("text/html"));
responseHeaders.setContentLength(array.length);
responseHeaders.set("Content-disposition", fileName);
return new ResponseEntity<byte[]>(array, responseHeaders, HttpStatus.OK);
}
は今、私はjQueryの/ AJAXを経由して、それを呼び出すしたいと思いますが、私が持っているものです。
<button class="btn" id="downloadButton" name="23">download</button>
$('#downloadButton').click(function() {
var urlToCall = "/download/"+this.name;
$.ajax({
type:'GET',
url: urlToCall,
success: function(data){
}
});
});
このコードはJavaメソッドを呼び出してからJS成功関数を呼び出しますが、ファイルには がダウンロードされていません。 @ResponseBodyで試したが、何も変わっていない。私は何を取りこぼしたか?