I am running into an issue trying to implement the download of spreadsheets served by my Spring Boot-based server through my Angular (v11) web app. My app offers some 'larger' spreadsheets for download (~32 MB), and the browser is sometimes crashing while attempting to download those files.
Here's the basics of my code - first, the RESTful client in my Angular app:
export class RestService {
private url: string = "http://example.com/api/dowwnload";
constructor(private http: HttpClient) {}
requestFile(id: number): Observable<HttpResponse<any>> {
this.http.get<any>(url+"?id="+id, {observe: 'response', responseType: 'blob' as 'json'});
}
}
...and the code that is trying to do the downloading (using file-saver):
this.restService.requestFile(id).subscribe(res => {
// requires: import {saveAs} from 'file-saver';
saveAs(res.body, 'filename.xlxs');
});
Here is the endpoint on the server side:
@GetMapping("/download")
public ResponseEntity<Resource> downloadFile(@RequestParam("id") int id) {
File out = exportFileToTmpById(id); // method that writes file to /tmp here
try {
FileSystemResource resource = new FileSystemResource(out);
HttpHeaders headers = new HttpHeaders();
headers.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + out.getName() + "\"");
return ResponseEntity.ok()
.headers(httpHeaders)
.contentLength(out.length())
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.body(resource);
}
catch (Exception e) {
e.printStackTrace();
}
}
My intuition is that there is either a memory issue or a race condition occurring on the Angular side, but I am not an expert on any of this stuff (#programmingbystackoverflow). The browser (Chrome & Firefox) dies with no error message, and no relevant messages are written to Chrome's log. Should I be removing the child "a" element after the click?
Please let me know if you need any more details in order to help me solve this problem.