I have .xlsx, .docx and .pdf files saved on my backend. The download controller looks like this:
@RestController
public class FileDownload {
@RequestMapping(value = "/files/{file_name}/", method = RequestMethod.GET, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public void getFile(
@PathVariable("file_name") String file,
HttpServletResponse response) throws Exception {
String fileType=file.split("\\.")[1];
switch(fileType){
case "xlsx": response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
break;
case "docx": response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
break;
case "pdf": response.setContentType("application/pdf");
break;
default: response.setContentType("application/octet-stream");
break;
}
response.setHeader("Content-Disposition", "attachment; filename=" + file);
response.setHeader("Content-Length", String.valueOf(file.length()));
InputStream is = FileDownload.class.getResourceAsStream("/files/" + file);
copy(is, response.getOutputStream());
response.flushBuffer();
}
}
When trying to open the file, excel tells me it is corrupted and needs to be repaired. The repairing process fails too though.
What could be causing this error?
There are a lot of similar questions but none of the proposed solutions seem to work.
The only "fix" that works is appending a "download" attribute to the link in the frontend which downloads the file. But sadly this doesn't work for IE.