0👍
From Spring you can return an array of bytes:
File f = new File("/path/to/the/file");
byte[] file = Files.readAllBytes(f.toPath());
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Disposition", "attachment; filename=\"" + f.getName() +".wav\"");
ResponseEntity<byte[]> response = new ResponseEntity(file, headers, HttpStatus.OK);
return response;
and then get it in javascript like so:
import fileDownload from 'js-file-download';
const { data } = await axios.get(`http://api.url`, {
responseType: 'arraybuffer',
headers: { 'Accept': '*/*', 'Content-Type': 'audio/wav' }
}).then(resp => resp);
const blob = new Blob([data], {
type: 'audio/wav'
})
const url = URL.createObjectURL(blob);
fetch(url)
.then(res => res.blob())
.then(blob => fileDownload(blob, 'your_filename'))
.catch(e => console.log('ERROR DOWNLOADING AUDIO FILE'));
I used React and axios, but I hope you’ll manqge 😉
Source:stackexchange.com